首页 > 解决方案 > 更新 Spree 用户自定义属性时出错

问题描述

我正在尝试向我的 Spree 用户添加自定义字段,例如名字、姓氏、avatar_url 和 bio。

我成功地将字段添加到表中:

class AddFieldsToSpreeUser < ActiveRecord::Migration[6.0]
  def change
    add_column :spree_users, :first_name, :string
    add_column :spree_users, :last_name, :string
    add_column :spree_users, :bio, :text
    add_column :spree_users, :avatar_url, :string
  end
end

我检查schema并按计划更新:

  create_table "spree_users", id: :serial, force: :cascade do |t|
    t.string "encrypted_password", limit: 128
    t.string "password_salt", limit: 128
    t.string "email"
    t.string "remember_token"
    t.string "persistence_token"
    t.string "reset_password_token"
    t.string "perishable_token"
    t.integer "sign_in_count", default: 0, null: false
    t.integer "failed_attempts", default: 0, null: false
    t.datetime "last_request_at"
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string "current_sign_in_ip"
    t.string "last_sign_in_ip"
    t.string "login"
    t.integer "ship_address_id"
    t.integer "bill_address_id"
    t.string "authentication_token"
    t.string "unlock_token"
    t.datetime "locked_at"
    t.datetime "reset_password_sent_at"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "spree_api_key", limit: 48
    t.datetime "remember_created_at"
    t.datetime "deleted_at"
    t.string "confirmation_token"
    t.datetime "confirmed_at"
    t.datetime "confirmation_sent_at"
    t.string "first_name"
    t.string "last_name"
    t.text "bio"
    t.string "avatar_url"
    t.index ["bill_address_id"], name: "index_spree_users_on_bill_address_id"
    t.index ["deleted_at"], name: "index_spree_users_on_deleted_at"
    t.index ["email"], name: "email_idx_unique", unique: true
    t.index ["ship_address_id"], name: "index_spree_users_on_ship_address_id"
    t.index ["spree_api_key"], name: "index_spree_users_on_spree_api_key"
  end

我将新属性添加到 Spree 初始化程序:

Spree::PermittedAttributes.user_attributes << [:first_name]
Spree::PermittedAttributes.user_attributes << [:last_name]
Spree::PermittedAttributes.user_attributes << [:bio]
Spree::PermittedAttributes.user_attributes << [:avatar_url]

当我尝试在rails c新属性中创建一个新用户时,它会作为模型的一部分出现。

我有以下表格来尝试更新用户:

<%= simple_form_for current_spree_user, url: spree.spree_user_registration_path(current_spree_user) do |f| %>
  <%= f.object.errors.full_messages.join(", ") if f.object.errors.any? %>

  <div class="row text-left">
    <div class="col-sm-6 py-3">
      <%= f.label "First Name" %>
      <%= f.text_field :first_name, class: "form-control", required: true, autofocus: true, input_html: { autocomplete: "fname" }  %>
    </div> <!-- col -->

    <div class="col-sm-6 py-3">
      <%= f.label "Last Name" %>
      <%= f.text_field :last_name, class: "form-control", input_html: { autocomplete: "lname" }  %>
    </div> <!-- col -->

    <div class="col-12 py-3">
      <%= f.label "Author Bio" %>
      <%= f.text_area :bio, class: "form-control" %>
    </div> <!-- col -->

    <div class="col-12 py-3">
      <%= f.label "Profile Image URL" %>
      <%= f.text_field :avatar_url, class: "form-control", input_html: { autocomplete: "lname" }  %>
    </div> <!-- col -->
  </div> <!-- row -->

  <div class="form-actions text-center">
    <%= f.button :submit, "Let's Go", class: "btn blue" %>
  </div>
<% end %>

当我点击提交时,我收到一个路由错误:

ActionController::UnknownFormat

我尝试过指定method: :patch,但得到了相同的结果。谁能看到我要去哪里错了?

标签: ruby-on-railsspreespree-auth-devise

解决方案


推荐阅读