如果有人能帮我解决以下问题,那就太好了。
我是初学者,目前遇到以下错误:ArgumentError in Users::MerchantsController#new "unknown keyword: :merchant_attributes"
使用 rails 7,设计和转换,我有一个用户表,以及消费者和商家等角色。对于商家角色,我喜欢通过子表添加地址信息,应通过单独的注册表单提交。
不知何故,我在控制器上遇到错误 - devise_parameter_sanitizer.permit -
:merchant_attributes
这就是它现在的样子。
用户.rb
class User < ApplicationRecord
has_one :user_merchant, dependent: :destroy
has_one :merchant, through: :user_merchant, dependent: :destroy
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
rolify
after_create :assign_default_role
accepts_nested_attributes_for :merchant, allow_destroy: true
商人.rb
class Merchant < ApplicationRecord
belongs_to :user
end
用户/merchants_controller.rb
# frozen_string_literal: true
class Users::MerchantsController < Devise::RegistrationsController
prepend_before_action :require_no_authentication, only: [:new, :create, :cancel]
before_action :configure_sign_up_params, only: [:new, :create]
before_action :configure_account_update_params, only: [:update]
# GET /merchants/sign_up
def new
# super
super do |resource|
resource.merchant.build
end
end
# POST /merchants
def create
super
end
# GET /merchants/edit
def edit
super
end
# PUT /merchants
def update
super
end
# DELETE /merchants
# def destroy
# super
# end
# GET /resource/cancel
# Forces the session data which is usually expired after sign
# in to be expired now. This is useful if the user wants to
# cancel oauth signing in/up in the middle of the process,
# removing all OAuth session data.
# def cancel
# super
# end
protected
# If you have extra params to permit, append them to the sanitizer.
def configure_sign_up_params
devise_parameter_sanitizer.permit(
:sign_up, keys: [:email, :password],
merchant_attributes: [:user_id, :id,
:organization_name, :address, :postal_code,
:city, :first_name, :last_name, :phone
])
end
# # If you have extra params to permit, append them to the sanitizer.
# def configure_account_update_params
# devise_parameter_sanitizer.permit(:account_update, keys: [:email, :password])
# end
# # The path used after sign up.
# def after_sign_up_path_for(resource)
# super(resource)
# end
# The path used after sign up for inactive accounts.
# def after_inactive_sign_up_path_for(resource)
# super(resource)
# end
end
user_merchant.rb
<%= render "devise/shared/error_messages", resource: resource %>
<div class="cb-title">Sign Up Merchant</div>
<div class="content-box">
<%# form_for(resource, as: user, url: merchants_registrations_path(resource_name)) do |f| %>
<%= form_for(resource, as: :user, url: merchants_path) do |f| %>
<div class="px-4 py-4 grid gap-4">
<div>
<%= f.label :email, class: "sr-only" %>
<%= f.email_field :email, autofocus: true, autocomplete: "email", class: "form-field", placeholder: "Email" %>
</div>
<div>
<%= f.label :password, class: "sr-only" %>
<%= f.password_field :password, autocomplete: "new-password", class: "form-field", placeholder: "Password" %>
</div>
<div>
<%= f.label :password_confirmation, class: "sr-only" %>
<%= f.password_field :password_confirmation, autocomplete: "Repeat password", class: "form-field", placeholder: "Repeat password" %>
</div>
<fieldset>
<legend>Merchant</legend>
<% f.fields_for :merchant do |mf| %>
<div class="col-span-full">
<%= mf.label :organization_name, class: "sr-only" %>
<%= mf.text_field :organization_name, autofocus: true, autocomplete: "organization", class: "form-field", placeholder: "Name business" %>
</div>
<div class="col-span-full">
<%= mf.label :address, class: "sr-only" %>
<%= mf.text_field :address, autocomplete: "street-address", class: "form-field", placeholder: "Address" %>
</div>
<%# ... %>
<div class="col-span-full">
<%= mf.label :phone, class: "sr-only" %>
<%= mf.telephone_field :phone, autocomplete: "tel", class: "form-field", placeholder: "Phone number" %>
</div>
<% end %>
</fieldset>
</div>
<div class="px-4 py-4 grid gap-4 bg-gray-50 border-t border-gray-200">
<button type="submit" class="btn">
<%= f.submit "Sign up" %>
</button>
</div>
<% end #form %>
我不知道哪里出错了,我查找了多个似乎做同样的例子。但显然我没有做对。