【问题标题】:Rails 4 has_and_belongs_to_manyRails 4 has_and_belongs_to_many
【发布时间】:2014-06-21 13:55:32
【问题描述】:

我有两个模型。场地和类别。它们相互关联为has_and_belongs_to_many

类别是预先填充的,并且在我想要显示多选的形式中,以允许在添加场地时选择场地的类别。

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

加入表格

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"]

大多数在线示例展示了如何从另一个模型创建模型。我无法弄清楚如何拥有一个多选选项,用户可以在其中选择一个或多个类别并自动保存。

我需要在控制器中使用builder 吗?并添加accepts_nested_attributes_for ?

我是 Rails 的新手,也一直在尝试搜索和阅读文档。

控制器

  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

查看

  <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>

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 relationship has-and-belongs-to-many


    【解决方案1】:

    问题在于他没有很好地建立关系。我们通过以下小改动对其进行了修复:

    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
    

    关于更新:

    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) }
    

    【讨论】:

    • has_many 是否“通过”?当我使用命名约定分配一个 habtm 时,我认为我不需要这样做吗?
    • 只要关系是通过一个连接表,你就必须用 :through 告诉 rails
    • 好的,谢谢。在视图中,我正在收集所有类别并将其传递给 :category_ids 但是当我尝试提交并保存它时说它需要一个数组但收到一个字符串。
    • 检查表单助手文档,如果问题仍然存在,请粘贴表单和参数请求,
    • 我一直用这个。但我认为你可以同时使用两者。阅读此文档:guides.rubyonrails.org/…
    【解决方案2】:

    您可以遍历所有类别

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

    产品的替代场所。

    这里的关键是表单元素的命名...

    如果你想进行多选,你可以使用一个选择......相应地命名它...... Venue[category_ids][] 你必须设置它以允许多选..我认为你的模型设置正确..

    抱歉在手机上,所以代码没有被格式化

    【讨论】:

    • 当我尝试命名多选时,它给了我错误。你能告诉我如何命名这个 true }, { :class=> 'select-search', :selected => params[:user_id], :data => { :placeholder => '请选择' } }) %>
    猜你喜欢
    • 2014-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-14
    • 2013-12-12
    • 2014-12-17
    相关资源
    最近更新 更多