2014-11-05 27 views
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"=>"[email protected]", "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。

回答

3

根據我的理解,您正在註冊新用戶。這裏是你的錯誤

= form_for(resource, as: resource_name, class: "form-signin input-medium", url: session_path(resource_name)) do |f| 
您使用 session_path這意味着你要「登錄」不爲「註冊」

。因此改變它

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