Rails 4 - checkboxes for has_and_belongs_to_many association

后端 未结 3 2017
悲&欢浪女
悲&欢浪女 2020-12-01 12:29

I recently had a problem getting checkboxes to work for a has_and_belongs_to_many (HABTM) association in Rails 4. I was able to find the information on how to get it working

相关标签:
3条回答
  • 2020-12-01 13:03

    The form should have something like this:

    <%= form_for(@kennel) do |form| %>
        ...
        <div class="field">
            <div class="field_head">Handlers</div>
            <%= hidden_field_tag("kennel[handler_ids][]", nil) %>
            <% Handler.order(:name).each do |handler| %>
                <label><%= check_box_tag("kennel[handler_ids][]", id, id.in?(@kennel.handlers.collect(&:id))) %> <%= handler.name %></label>
            <% end %>
        </div>
        ...
    <% end %>
    

    The hidden_field_tag allows the user to uncheck all the boxes and successfully remove all the associations.

    The controller needs to allow the parameter through strong parameters in the permitted_params method:

    params.permit(kennel: [:city, :state
        {handler_ids: []},
        :description, ...
        ])
    

    References:

    • http://railscasts.com/episodes/17-habtm-checkboxes
    • https://coderwall.com/p/_1oejq
    0 讨论(0)
  • 2020-12-01 13:15

    This is all you need to do for the form: Don't do it manually when there is a built in helper.

    <%= form_for @kennel do |f| %>
      <%= f.collection_check_boxes(:handler_ids, Handler.all, :id, :to_s) %>
    <% end %>
    
    0 讨论(0)
  • 2020-12-01 13:18

    I implement has_and_belongs_to_many association this way:

    model/role

    class Role < ActiveRecord::Base
      has_and_belongs_to_many :users
    end
    

    model/user

    class User < ActiveRecord::Base
      has_and_belongs_to_many :roles
    end
    

    users/_form.html.erb

    ---
    ----
    -----
     <div class="field">
            <% for role in Role.all %>
                <div>
                    <%= check_box_tag "user[role_ids][]", role.id, @user.roles.include?(role) %>
                    <%= role.name %>
                </div>
            <% end %>
        </div>
    

    users_controller.rb

    def user_params
        params.require(:user).permit(:name, :email, { role_ids:[] })
      end
    

    Intermediate table_name should be roles_users and there should be two fields:

    1. role_id
    2. user_id
    0 讨论(0)
提交回复
热议问题