ruby - Rails 4- Associations -Tutorial Movielist -
hi trying use rails on rest 2 - movielist tutorial rails 4 , making adjustments go new rails. stuck on associations of movies roles. have added movie.rb
class movie < activerecord::base has_many :roles, :dependent => :destroy has_many :people, :through => :roles validates_presence_of :title def new_role=(values) values.each |i, hash| unless hash[:name].blank? roles.create(:person_id => hash[:person_id], :name => hash[:name]) roles.save end end end def deleted_roles=(values) values.each |role_id| roles.find(role_id).destroy end end end
and show _form.html.rb render (excert below)
<b>add new people</b><br /> <% (1..3).each |i| %> <%= select_tag 'movie_new_role_person_id', options_for_select(@people), { :name => "movie[new_role][#{i}][person_id]" } %> <%= text_field_tag 'movie_new_role_name', '', { :name => "movie[new_role][#{i}][name]" } %><br /> <% end %> </p> <p> <%= f.submit "update" %> </p>
it renders list choose when submit - nothing written database table. if manually enter data in database displays on movielist page ie: "stephen spielberg - director" etc... appreciated- driving me nuts @ stage
i thought might params driven restriction not have example of associations style params filter
thanks
alan
after debugging including chicagogrrl's !flag investigated params.permits in movies_controller again , figured have add method types list of excepted. not find detailed info on syntax trial , error paid off.
app/controllers/movies_controllers.rb (excerpt) ...... # never trust parameters scary internet, allow white list through. def movie_params params.require(:movie).permit(:title,:description, :rating, :role, :deleted_roles=>[], :new_role=> ['person_id', 'name']) end
......
the delete_roles=>[] takes array params , process delete_roles method in movies.rb new_role=>['person_id', 'name'] takes new_role individual params.
i hope saves else time andif needs anymore info ask again alan
Comments
Post a Comment