Search code examples
ruby-on-rails-4parameterscontrollerbelongs-to

Getting "Unpermitted parameter: address" even though I included field in my "permit" statement


I’m using Rails 4.2.3. I have this in my 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

and when my parameters get submitted to “update,”

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

Why am I getting this unpermitted parameter message when it is included in my “require” statement above? I even have this in my app/models/user.rb file …

class User < ActiveRecord::Base
  …
  belongs_to :address

Solution

  • There are 2 issues that are linked. (My assumption is that the user has one address, and not that the address has one user.) The model relationship should be:

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

    The address model:

    class Address < ActiveRecord::Base
      …
      belongs_to :user
    

    This should eliminate the unpermitted parameter error for address.