为deivse获取“Unpermitted parameters”错误

时间:2014-11-05 13:49:37

标签: ruby-on-rails-4 devise ruby-on-rails-4.1

您正在使用示例应用,我正在使用设计进行身份验证。 我在注册时添加了以下额外参数。

:first_name, :last_name, :mobile, :gender, :address

但是我在注册新用户时遇到Unpermitted parameters: first_name, last_name, password_confirmation错误。

我评论了以下链接 Add Custom Field/Column to Devise with Rails 4

http://www.jacopretorius.net/2014/03/adding-custom-fields-to-your-devise-user-model-in-rails-4.html

但它没有奏效。这不是代码集

user.rb

class User < ActiveRecord::Base

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  validates_confirmation_of :password, :only => :create
end

我也在应用程序控制器中实现了它,但它没有工作,所以我创建了单独的 登记控制员。

registration_controller.rb

class RegistrationsController < Devise::RegistrationsController

  before_filter :configure_permitted_parameters

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) do |u|      
      u.permit(:first_name, :last_name, :mobile, :gender, :address, :email, :password, :password_confirmation)
    end
    devise_parameter_sanitizer.for(:account_update) do |u|
      u.permit(:name,
      :email, :password, :password_confirmation, :current_password)
    end
  end
end

application_controller.rb

class ApplicationController < ActionController::Base

  protect_from_forgery with: :exception

  private
  def after_sign_in_path_for(resource)
    user_landing_page
  end

  def user_landing_page
    contact_us_path      
  end
end

设计/注册/ new.html.haml

.container
  .row
    .col-sm-6.col-md-4.col-md-offset-4
      %h1.text-center.login-title Create New Account
      .account-wall
        %img.profile-img{:alt => "", :src => "https://lh5.googleusercontent.com/-b0-k99FZlyE/AAAAAAAAAAI/AAAAAAAAAAA/eu7opA4byxI/photo.jpg?sz=120"}
          = form_for(resource, as: resource_name, class: "form-signin input-medium", url: session_path(resource_name)) do |f|
            = f.text_field :first_name, class: "form-control", placeholder: "First Name", autofocus: true
            = f.text_field :last_name, class: "form-control", placeholder: "Last Name", autofocus: true
            = f.email_field :email, class: "form-control", placeholder: "Email", autofocus: true
            = f.password_field :password, class: "form-control", placeholder: "Password", autocomplete: "off"
            = f.password_field :password_confirmation, class: "form-control", placeholder: "Confirm Password", autocomplete: "off"
            = f.submit "Log in", class: "btn btn-lg btn-primary btn-block login-button"

development.log

Parameters: {"utf8"=>"✓", "authenticity_token"=>"pe7wBW3iWnn39p3nJAi8utbuECj+x8zX/pIxr/6sKbo=", "user"=>{"first_name"=>"first_name", "last_name"=>"last_name", "email"=>"admin@my.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Log in"}
Unpermitted parameters: first_name, last_name, password_confirmation
  Rendered devise/sessions/new.html.haml within layouts/application (6.6ms)
  Rendered layouts/_home_header.html.haml (1.2ms)
Completed 200 OK in 426ms (Views: 316.8ms | ActiveRecord: 0.0ms)

的routes.rb

Rails.application.routes.draw do
  root :to => 'landing#index'  
  devise_for :users, :controllers => {:registrations => "registrations"}
  get '/about_us' => 'statics#about_us', as: :about_us
  get '/contact_us' => 'statics#contact_us', as: :contact_us
end

任何人都可以建议我缺少什么。 我正在使用Rails 4.1.4,ruby 2.1.2和设计3.4.1。

1 个答案:

答案 0 :(得分:3)

据我了解,您正在注册新用户。这是你的错误

= form_for(resource, as: resource_name, class: "form-signin input-medium", url: session_path(resource_name)) do |f|

您正在使用session_path,这意味着您将要登录&#34;登录&#34;不是&#34;注册&#34;。所以改变它

= form_for(resource, as: resource_name, class: "form-signin input-medium", url: registration_path(resource_name)) do |f|