使用devise进行注册时ActiveModel :: ForbiddenAttributesError

Thr*_*esh 3 ruby-on-rails devise strong-parameters ruby-on-rails-4

我正在使用设计来注册我的用户.我正在自定义注册控制器的创建操作,但是每当我尝试创建任何新用户时,它都被拒绝,我得到上述错误.我知道我必须允许所需的参数,我这样做我不明白为什么我一直收到错误.到目前为止,贝娄是我的考验.

#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
Run Code Online (Sandbox Code Playgroud)

现在每当我创建一个新用户时,我在标题中得到了提到的错误,并且发布的puts语句的结果如下:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
email
password
password_confirmation
first_name
last_name
country_id
city
dob
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Run Code Online (Sandbox Code Playgroud)

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

Uel*_*elb 6

问题出在这一行:

@user = User.new(params[:user])
Run Code Online (Sandbox Code Playgroud)

相反,也许你应该尝试

@user = User.new sign_up_params
Run Code Online (Sandbox Code Playgroud)

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

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

build_resource sign_up_params
Run Code Online (Sandbox Code Playgroud)

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