2011-12-30 26 views
2

我&范畴的分类项目的关联:的has_many:通过将不保存到数据库

class Item < ActiveRecord::Base 
    has_many :categorizations 
    has_many :categories, :through => :categorizations, :source => :category 
end 


class Category < ActiveRecord::Base 
    has_many :categorizations 
    has_many :items, :through => :categorizations, :source => :item 
    attr_accessible :name 
end 

class Categorization < ActiveRecord::Base 
    belongs_to :item 
    belongs_to :category 
end 

项目/新:

<div class="container"> 
<%= render 'shared/error_create_item_messages'%> 
<br/> 

<%= form_for(@item, :html => {:multipart => true}) do |f| %> 

    <div class="clearfix">  
     <label> 
     <%= f.label :name %> 
     </label> 
     <div class="input">  
     <%= f.text_field :name %> 
     </div> 
    </div> 


    <div class="clearfix"> 
     <label> 
     <%= f.label :category %> 
     </label> 

     <%= hidden_field_tag "product[category_ids][ ]", nil %>  
     <% Category.all.each do |category| %> 
      <div class="input">   
       <%= check_box_tag "item[category_ids][ ]", category.id, 
              @item.category_ids.include?(category.id) %> 
       <%= category.name %> 
      </div>  
     <% end %> 
    </div>  

    <div class="action"> 
     <div class="btn_create_item_align"> 
     <%= f.submit "Create item", :class=>"btn primary" %> 
     </div> 
    </div> 

<% end %> 
</div> 

Categorizations_controller

class CategorizationsController < ApplicationController 
    def create 
    @categories = Category.all 
    Categorization.create(:item_id => item.id, :category_id => category.id) 
    Categorization.save 
    end 

    def edit 
    end 

end 

Items_controller

def create 
    @item = @current_user.items.build(params[:item]) 
    @categories = Category.all 
    if @item.save 
     redirect_to @item 
    else 
     render 'new' 
    end 
end 

问题是当我点击保存(创建项目),我检查分类表并检查控制台上,保存的项目仍然没有category_id。 因此,新项目及其属性(名称,说明,价格)已正确保存到数据库,但不是类别。它不会保存到分贝。

任何想法? (Newbie in Rails) 谢谢

+0

你的控制器的创建动作是什么样的? – rkb 2011-12-30 05:14:14

+0

Hi @rkb,刚刚编辑添加控制器。我希望它能帮助我解决这个问题。 – imseto 2011-12-30 06:40:57

回答

1

表单POST到ItemsController#create,而CategorizationsController#create没有被调用(您可以使用puts debugging来验证这一点)。

您可以使用accepts_nested_attributes_for让项目的创建操作完成所有工作。诀窍是仅创建类别关联,其复选框被选中,你可以做到这一点的:reject_if选项(see Rails API doc for more info):

应用程序/模型/ item.rb的:

class Item < ActiveRecord::Base 
    has_many :categorizations 
    has_many :categories, :through => :categorizations, :source => :category 
    accepts_nested_attributes_for :categories, 
           :reject_if => proc{|c| c[:persist].blank?} 
end 

然后您可以创建表格嵌套对象的字段,每个类别一个复选框。

应用程序/视图/项目/ new.html.erb:

<%= form_for @item do |f| %> 
    <%# stuff to generate item fields... %> 

    <%= f.fields_for :categories do |cat| %> 
    <%= cat.check_box :persist %> 
    <%= cat.label :name, cat.name %> 
    <%- end %> 

    <%# submit button, etc. %> 
<%- end %> 

填充类别集由建设(但不保存)与项目相关的分类创建一个新的项目时进行选择。这有效地从您的视图移动代码到控制器:

应用程序/控制器/ items_controller.rb:

def new 
    @item = Item.new 
    Category.all.each {|cat| @item.categories.build(cat.attributes) } 
end 

这将是教育,以puts在控制器动作params,所以你可以看到什么从窗体发送的哈希看起来像。

+0

哇感谢@rkb,我仍然试图在这里消化你的解释,阅读API和相关的东西。再次感谢,我会尽快回复。 – imseto 2011-12-30 10:30:34

+0

这仍然是最新的解决方案吗?我得到一个'坚持'的方法不存在。 – 2015-10-05 12:11:29

相关问题