Rails 4 has_and_belongs_to_many

不问归期 提交于 2019-12-08 04:32:46

问题


I have two models. Venues and Categories. They are related to each other as has_and_belongs_to_many

Categories are pre populated and in the form i want to display a multi-select to allow choosing the categories for a venue while adding a venue.

venue.rb

class Venue < ActiveRecord::Base

  has_and_belongs_to_many :categories

end

category.rb

class Category < ActiveRecord::Base

  has_and_belongs_to_many :venues

end

Join Table

create_table "categories_venues", id: false, force: true do |t|
  t.integer "category_id", null: true
  t.integer "venue_id",    null: true
end

add_index :categories_venues, ["category_id", "venue_id"]

Most of the examples online show how to create the models from with another. I am not able to figure out how to have a multi select option where the user can select one or more categories and save it automatically.

Do i need to use builder in the controller? and add accepts_nested_attributes_for ?

Am new to Rails and been trying to search and read through the docs as well.

Controller

  def new
    @venue = Venue.new

    @categories = Category.all.order('name ASC')
    @countries = Country.all.order('name ASC').limit(25)
    @regions = Region.all.order('name ASC').limit(25)
    @cities = City.all.order('name ASC').limit(25)

    #render plain: @categories.inspect

  end

View

  <div class="form-group">
    <%= f.label :parent_id, "Categories:<span class='mandatory'>*</span>".html_safe,:class => 'col-sm-2 control-label' %>
    <div class="col-sm-3">
      <%= f.collection_select(:category_ids, @categories, :id, :name, { :prompt => true }, { :class => 'select-search', :selected => params[:user_id], :data => { :placeholder => 'Please Choose' } }) %>
      <%= show_errors(@venue, :category_ids).html_safe %>
    </div>
  </div>

回答1:


Well the problem was that he wasn't creating the relationships well. We've fixed it with this small changes:

def category_ids
  params.permit(category_ids: [])
end

def venue_params
   #removed category_ids from permit
end

def create
   @venue = Venue.new(venue_params)
   if @venue.save
     category_ids.each {|id| @venue.categories << Category.find(id)}
   # rest of the code
end

And for the update:

def update
   @venue = venue.find(params[:id])
   if @venue.update(venue_params)
      @venue.categories.delete_all
      category_ids.each {|id| @venue.categories << Category.find(id) }



回答2:


You can loop over all the categories

= check_box_tag "product[category_ids][]", category.id, @product.categories.include?(category)

Substitute venue for product.

The key here is the naming of the form element...

If you want to do a multi select you can use a select ... Name it accordingly... Venue[category_ids][] and you would have to set it up to allow multi select .. I think your models are set correctly by the way..

Sorry in on a mobile so code is not being formatted



来源:https://stackoverflow.com/questions/24342275/rails-4-has-and-belongs-to-many

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!