gpt4 book ai didi

ruby-on-rails - 无法在 Rails 4 中检索 attr_accessor

转载 作者:数据小太阳 更新时间:2023-10-29 08:27:12 26 4
gpt4 key购买 nike

我正在做一个 Ruby on Rails 项目,我有两个模型:

<强>1。用户

<强>2。商家

他们的关系是:

用户 has_one :merchant

Merchant belongs_to :user

然后在我的 user.rb

attr_accessor :merchant_name
after_create :create_merchant

def create_merchant
create_merchant(name: merchant_name)
end

在我的用户表单中:

= form_for @user do |f|
= f.text_field :merchant_name
= f.text_field :email
= f.text_field :password

问题是用户和商家已经创建但是商家的名字是nil

在我的Account::RegistrationsController

class Account::RegistrationsController < Devise::RegistrationsController

protected
def after_sign_up_path_for(resource)
account_users_path
end


private

def registration_params
params.require(:user).permit(:merchant_name)
end

end

我收到这个错误:

  Processing by Account::RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"ht8vHG8I4bz2ylt+mtLC7hilQnK3VnYtcHgSNv8nSeg=", "user"=>{"merchant_name"=>"Zara 123", "email"=>"allen.chun@hotmail.com",
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign Up"}

Unpermitted parameters: merchant_name

最佳答案

如果我是你,我不会重写 create_merchant 方法,试试这样的方法:

after_create -> { create_merchant!(:name => self.merchant_name) }

并确保您的 merchant_name 在用户 Controller 中是允许的,如下所示:

def create
@user = User.new(user_params)
if @user.save
redirect_to @user
else
render :new
end
end

private

def user_params
params.require(:user).permit(:merchant_name, :email, :password, :password_confirmation)
end

关于ruby-on-rails - 无法在 Rails 4 中检索 attr_accessor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24230362/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com