0

I have a form:

= simple_form_for @character do |f|
    = f.input :name
    = f.input :description
    = f.simple_fields_for @movie_characters do |fmc|
        = fmc.association :movie, value_method: :id, label_method: :title
    = f.submit

Relationship here is Character has many MovieCharacters and MovieCharacter belongs_to Movie.

params I get from this form look like this:

{"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"6d8n1Z5AC3nF/cqzTInnMr5TyRhZFcGV8ssf5CsNkjskiD9lUL10nVDYeV4i0yS85Q87yNcIi+coFFDf0mpZ4w==", "character"=>{"name"=>"Joker", "description"=>"best villain ever", "movie_character"=>{"movie_id"=>"1"}}, "commit"=>"Update Character", "controller"=>"characters", "action"=>"update", "id"=>"1"}

character_params method looks like this:

def character_params
        params.require(:character).permit(:name, :description, movie_characters_attributes: [:movie_id, :character_id])
    end

Unfortunately while calling this method in controller I get "Unpermitted parameter: movie_character" warning. I also tried

def character_params
            params.require(:character).permit(:name, :description, movie_characters: [:movie_id, :character_id])
        end

But effects are the same. Can you tell me why it doesnt work?

UPDATE:

class Character < ActiveRecord::Base
    has_many :movie_characters

    accepts_nested_attributes_for :movie_characters
end

class MovieCharacter < ActiveRecord::Base
  belongs_to :character
  belongs_to :movie
end
Leo
  • 2,061
  • 4
  • 30
  • 58

1 Answers1

0

You have a typo. It should be:

def character_params
    params.require(:character).permit(:name, :description, movie_character_attributes: [:movie_id, :character_id])
end

without "s" in movie_characters_attributes.

Radek
  • 202
  • 2
  • 6
  • on my side its not a typo, take a look at the example on railsguides: http://edgeguides.rubyonrails.org/action_controller_overview.html#strong-parameters – Leo May 18 '15 at 17:45
  • Okay, but see what you send to the controller. There is a `movie_character` param, so you can try solution suggested by me and check if it works. – Radek May 18 '15 at 17:48