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

标签 ruby-on-rails ruby-on-rails-4 devise strong-parameters

我正在使用设计来注册我的用户。我正在自定义注册 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/

相关文章:

ruby-on-rails - 如何将验证与模型分开

ruby-on-rails - 如果用户未登录设备,则重定向到注册页面

css - 是否可以使用 Twitter Bootstrap 或 CSS 选择器淡出 Devise 警报?

ruby-on-rails - Carrierwave 不会在模型更新后重新创建版本

ruby-on-rails - 在创建时使用 set_callback 进行 after_commit

ruby-on-rails - 如何在数据库中存储当前日期当前时间

ruby-on-rails - Rails 4 销毁操作,undefined_method message_path

i18n-js 的 Heroku Assets 预编译失败

ruby - 通过 Rails 控制台获取模型名称会给出不同的结果

ruby-on-rails - 尝试在 ruby​​ on rails 中重命名设计模型名称