gpt4 book ai didi

ruby-on-rails - ActiveModel::ForbiddenAttributesError 使用设备进行注册时

转载 作者:行者123 更新时间:2023-12-05 00:26:10 29 4
gpt4 key购买 nike

我正在使用设计来注册我的用户。我正在自定义注册 Controller 的创建操作,但是每当我尝试创建任何新用户时,它都会被拒绝,并且出现上述错误。我知道我必须允许所需的参数,而且我这样做了,所以我不明白为什么我不断收到错误消息。以下是我迄今为止的试验。

#user model
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable
belongs_to :country
#rest of the code
end

## Custom registeration controller
class RegistrationsController < Devise::RegistrationsController
def create
@user = User.new(params[:user])
if @user.save
sign_in(@user, bypass: true)
redirect_to user_employee_steps_path(@user),
notice: 'A confirmation email was sent to your email, please complete your profile
and confirm your account to start looking for potential employees'
else
render 'new'
end
end
end

#new.html.erb the view for the registration sign up
<h2>Sign up</h2>

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>

<div><%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %></div>

<div><%= f.label :first_name %><br />
<%= f.text_field :first_name %></div>

<div><%= f.label :last_name %><br />
<%= f.text_field :last_name %></div>

<div><%= f.label :dob, "Birthday" %><br />
<%= f.date_select :dob, start_year: 1900, end_year: Time.now.year - 18 %></div>

<div><%= f.label :country_id, "Country" %><br />
<%= f.select :country_id, Country.order(name: :asc).collect { |country| [country.name, country.id] }, prompt: 'Select your country' %></div>

<div><%= f.label :city %><br />
<%= f.text_field :city %></div>

<div><%= f.label :password %><br />
<%= f.password_field :password, autocomplete: "off" %></div>

<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %></div>

<div><%= f.submit "Sign up" %></div>
<% end %>

<%= render "users/shared/links" %>

#application controller where I permit params for sign up
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?

protected

def configure_permitted_parameters
puts '%' * 30
devise_parameter_sanitizer.for(:sign_up) << [:first_name, :last_name, :country_id, :city, :dob]
puts devise_parameter_sanitizer.for(:sign_up)
puts '%' * 30
end
end

现在每当我创建一个新用户时,我都会在标题中看到提到的错误,并且正在发布的 puts 语句的结果如下:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
email
password
password_confirmation
first_name
last_name
country_id
city
dob
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

这意味着方法 configure_permitted_pa​​rameters 被正确调用,并且所需的参数应该已经存在,所以我不明白问题可能出在哪里?!我真的坚持这一点,任何帮助将不胜感激。

最佳答案

问题出在这一行:

@user = User.new(params[:user])

相反,也许你应该尝试
@user = User.new sign_up_params

如果您想更好地理解原因,请阅读有关强参数和 Rails 4 的更多信息。
http://weblog.rubyonrails.org/2012/3/21/strong-parameters/

或者坚持设计处理,你也可以写
build_resource sign_up_params

然后资源变量将使您可以访问您可以保存的用户等...

关于ruby-on-rails - ActiveModel::ForbiddenAttributesError 使用设备进行注册时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22753243/

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