update values of checkbox with HABTM relationship — Rails

前端 未结 2 1096
旧巷少年郎
旧巷少年郎 2021-01-07 10:56

Hey guys I\'ve been using the has_and_belongs_to_many relationship with checkboxes example from the Railscast Episode #17 . I had some problems and now everything is workin

2条回答
  •  日久生厌
    2021-01-07 11:44

    I played around with the form and the correct code is this ==>

    <% form_for @user do |f| %>
    <% for interest in Interest.find(:all) %>
    
    <%= check_box_tag "user[interest_ids][]", interest.id, @user.interests.include?(interest) %> <%= interest.name %>
    <% end %>

    <%= f.submit 'Edit' %>

    <% end %>

    I'm so happy! So apparently the problem was :user which should the be object @user.

提交回复
热议问题