[ActiveModel :: ForbiddenAttributesError,而使用devise进行注册时

问题描述 投票:4回答:2

我正在使用装置来注册我的用户。我正在自定义注册控制器的创建动作,但是,每当我尝试创建任何新用户时,它都会被拒绝,并且出现上述错误。我知道我必须允许所需的参数,但是我不明白为什么我会不断出错。以下是到目前为止我的试用。

#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方法,并且所需的参数应该已经存在,所以我不知道问题可能在哪里?!我真的很坚持这一点,任何帮助将不胜感激。

ruby-on-rails ruby-on-rails-4 devise strong-parameters
2个回答
6
投票

问题出在这行:

@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

然后,资源变量将为您提供可以保存的用户访问权限,等等...


0
投票

@@ thresh:尽管这与您的代码并非100%并行,但错误类似,并且此修复使用@Uelb建议使用“强参数”可能会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.