获取“未经许可的参数:地址”,即使我在“许可”声明中包含了字段

时间:2016-07-02 19:31:41

标签: ruby-on-rails-4 parameters controller belongs-to

我正在使用Rails 4.2.3。我在我的user_controller.rb ...

中有这个
  def update
    @user = current_user
    if @user.update_attributes(user_params)
      flash[:success] = "Profile updated"
      redirect_to url_for(:controller => 'races', :action => 'index') and return
    else
      render 'edit'
    end
  end

  private

    def user_params
      params.require(:user).permit(:first_name, :last_name, :dob, :address, :automatic_import)
    end

当我的参数提交到“更新”时,

Processing by UsersController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"bz24jRzK5qVWolW0oBoQbniiaQi96MeFFmzNjB4kCr0ifUYSsitvQIyod5AWDJd4KS3lrt3mzLiG8F9ef3aJkg==", "user"=>{"first_name"=>"Anthony", "last_name"=>"Alvarado", "dob(2i)"=>"1", "dob(3i)"=>"14", "dob(1i)"=>"1908", "address"=>{"city"=>"chicago"}, "automatic_import"=>"0"}, "state"=>"CT", "country"=>{"country"=>"233"}, "commit"=>"Save", "id"=>"13"}
Unpermitted parameter: address

为什么我在上面的“require”语句中包含这个未经许可的参数消息?我甚至在我的app / models / user.rb文件中有这个......

class User < ActiveRecord::Base
  …
  belongs_to :address

1 个答案:

答案 0 :(得分:0)

有两个相关的问题。 (我的假设是用户有一个地址,而不是地址有一个用户。)模型关系应该是:

class User < ActiveRecord::Base
  …
  has_one :address
  accepts_nested_attributes_for :address

地址模型:

class Address < ActiveRecord::Base
  …
  belongs_to :user

这应该消除地址的未允许参数错误。