ruby-on-rails - 更新 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
,但得到了相同的结果。谁能看到我要去哪里错了?
解决方案
推荐阅读
- python - python如何在except块情况下捕获自定义异常?
- java - 将结果 ResultInfo{who=null, request=13001, result=-1, data=Intent { act=inline-data (has extras) }} 传递给活动失败
- php - 创建事件注册重复事件
- javascript - 当下一行用括号包裹时,Eslint 和 Prettier 没有半规则会导致行执行
- ios - 通过故事板命名颜色
- java - 是否可以在命令行/作为 web 服务中运行 java 应用程序
- elasticsearch - 使用 elasticquent 库创建弹性搜索映射时出错
- r - 如何在 R 中安装特定版本的“rlang”包?
- swift - Swift 结构定义中的崩溃。有解决方法,但想提高理解
- codenameone - 代号一方形按钮