2016-01-18 118 views
5

我为我的网站创建了一个注册表单,并将下列参数包含在我的users_controller.rb文件中,以便将输入到相应字段中的数据保存到我的数据库中。出于某种原因,当我尝试提交创建的注册表单时,控制台告诉我名字和姓氏是不允许的参数。错误是:Rails中的未经允许的参数

不允许的参数:名字,姓氏

的注册成功,而其他参数输入到数据库就好了;但名字和姓氏列保持空白。

users_controller.rb

class UsersController < ApplicationController 

def create 
    @user = User.new(user_params) 
    if @user.save 
    flash[:success] = "You signed up successfully" 
    flash[:color] = "valid" 
    redirect_to @user 
    else 
    flash[:notice] = "Form is invalid" 
    flash[:color] = "invalid" 
    render "new" 
    end 
end 

private 
def user_params 
    params.require(:user).permit(:firstname, :lastname, :email, :password, :password_confirmation) 
end 


def show 
    @user = User.find(params[:id]) 
end 

def new 
end 

end 

new.html.erb

<%= form_for(:user, :url => {:controller => 'users', :action => 'create'}) do |f| %> 

    </br> <%= f.text_field :firstname, placeholder: 'First Name' %> 
    </br> <%= f.text_field :lastname, placeholder: 'Last Name' %> 
    </br> <%= f.text_field :email, placeholder: 'Email' %> 
    </br> <%= f.password_field :password, placeholder: 'Password' %> 
    </br> <%= f.password_field :password_confirmation, placeholder: 'Confirm Password' %> 


    <%= f.submit :Register %> 
    <% end %> 

schema.rb

create_table "users", force: :cascade do |t| 
    t.string "firstname" 
    t.string "lastname" 
    t.string "email" 
    t.datetime "created_at",  null: false 
    t.datetime "updated_at",  null: false 
    t.string "password_digest" 
    t.string "password" 
    end 

user.rb

class User < ActiveRecord::Base 

    has_secure_password 

    validates_length_of :password, :in => 6..20, :on => :create 
    validates :password_confirmation, presence: true, if: -> { new_record? || changes["password"] } 

end 

传入的参数(控制台)

Processing by UsersController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"1hHzXu2RZR3G3Zx4PoOeDu3DlU31V5abDH/UmNx+w9hs/gacgRYhFgpe6cm0d7cLaTtZdfROi3oUrw/m5EcTAQ==", "user"=>{"firstname"=>"Milhouse", "lastname"=>"Vanhoutten", "email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Register"} 
Unpermitted parameters: firstname, lastname, 
    (0.4ms) begin transaction 
    SQL (0.6ms) INSERT INTO "users" ("email", "password_digest", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["email", "[email protected]"], ["password_digest", "$2a$10$T.iv9b3BjG.t1FJvT7YsVOZf0wNOD2QSFA9lP8RGOiE1h5AaDdB2q"], ["created_at", "2016-01-18 22:12:32.764948"], ["updated_at", "2016-01-18 22:12:32.764948"]] 
    (7.0ms) commit transaction 
Redirected to /users/6 
Completed 302 Found in 122ms (ActiveRecord: 8.7ms) 
+0

你可以显示你的用户架构(从schema.rb文件)? – SteveTurczyn

+0

显示提交的params散列 –

+0

@SteveTurczyn请参阅上面添加的Schema。 – Brittany

回答

1

它看起来像参数可以在未来作为

{:firstname => 'foo', :lastname => 'baz'} 

,而不是

{"user" => {"firstname" => "foo", "lastname" => "baz"}} 

它可能可以追溯到表单。

+1

对不起newb响应 - 但为什么会发生这两个值,而不是例如电子邮件的价值?我怎样才能解决这个问题? – Brittany

+0

这实际上是公平的。你能显示传入的参数吗?它们应该在rails webserver输出中打印。 – errata

+0

看到上面的传入参数 - 非常感谢,非常感谢。 – Brittany

相关问题