2010-02-06 74 views
25

我有两个模型,链接和标签,通过第三个link_tags相关联。以下代码位于我的Link模型中。accepted_nested_attributes_for has_many =>:通过选项

协会:

class Link < ActiveRecord::Base 
    has_many :tags, :through => :link_tags 
    has_many :link_tags 

    accepts_nested_attributes_for :tags, :allow_destroy => :false, 
    :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } } 
end 

class Tag < ActiveRecord::Base 
    has_many :links, :through => :link_tags 
    has_many :link_tags 
end 

class LinkTag < ActiveRecord::Base 
    belongs_to :link 
    belongs_to :tag 
end 

links_controller操作:

def new 
    @link = @current_user.links.build 
    respond_to do |format| 
     format.html # new.html.erb 
     format.xml { render :xml => @link } 
    end 
    end 

    def create 
    @link = @current_user.links.build(params[:link]) 

    respond_to do |format| 
     if @link.save 
     flash[:notice] = 'Link was successfully created.' 
     format.html { redirect_to links_path } 
     format.xml { render :xml => @link, :status => :created, :location => @link } 
     else 
     format.html { render :action => "new" } 
     format.xml { render :xml => @link.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 

查看代码从new.html.erb:

<% form_for [current_user, @link], :url => account_links_path do |f| %> 
<%= render :partial => "form", :locals => { :f => f } %> 
<% end %> 

和相应的部分:

<%= f.error_messages %> 

    <p> 
    <%= f.label :uri %><br /> 
    <%= f.text_field :uri %> 
    </p> 
    <p> 
    <%= f.label :title %><br /> 
    <%= f.text_field :title %> 
    </p> 

    <h2>Tags</h2> 
    <% f.fields_for :tags_attributes do |tag_form| %> 
    <p> 
    <%= tag_form.label :name, 'Tag:' %> 
    <%= tag_form.text_field :name %> 
    </p> 
    <% unless tag_form.object.nil? || tag_form.object.new_record? %> 
    <p> 
    <%= tag_form.label :_delete, 'Remove:' %> 
    <%= tag_form.check_box :_delete %> 
    </p> 
    <% end %> 
    <% end %> 

    <p> 
    <%= f.submit 'Update' %> 
    </p> 

下面的代码行,在链路控制器的创建动作引发错误:

@link = @current_user.links.build(params[:link]) 

错误:Tag(#-621698598) expected, got Array(#-609734898)

是否有在的has_many需要额外的步骤=>:通过案例?这些似乎是基本哈萨克斯坦案例中唯一显示的变化。在新的行动(即加载形式部分),你建立通过你的链接@tag

<% f.fields_for :tags_attributes do |tag_form| %> 
+0

我无法根据您发布的代码重现您的问题。你可以发布你的三个模型的关联部分(has_many/belongs_to/etc行),这两个相关的控制器动作(链接#新,链接#创建)以及任何与链接形式有关的视图代码。 – EmFi 2010-02-06 15:52:04

+0

我已经添加了关联,控制器操作和视图的代码。谢谢你的帮助。现在 – 2010-02-06 19:33:16

回答

1

试试这个?

所以,你应该看到沿东西线:

@link = Link.new 
@tag = @link.tags.build 

这可能是最好的发布新的内容,并创建links_controller的作用。

+1

不同的错误: '未定义的方法'with_indifferent_access' 为 “编码”:字符串” 参数: { “提交”=> “更新”, “authenticity_token”=> “fwCGGgcTKOSfxpFJMXmq7IUfRtfOvdOKl31Xys4TKC8 =”, “链接“=> {”tags_attributes“=> {”name“=>”Coding“}, ”uri“=>”http://stackoverflow.com“, ”title“=>”Stack Overflow“}} 感谢您的建议。有针对这个的解决方法吗?它试图将其中一个属性的值视为散列吗? – 2010-02-06 09:42:41

+0

有没有办法为这个混乱得到适当的格式? – 2010-02-06 09:43:12

+0

你能否粘贴你的整个表格 – obiwanchinobi 2010-02-06 09:51:42

0

在你的控制器:

+0

控制器操作现在在原始帖子中。 我不是在新操作中创建标签。我在这里跟随多模型部分:http://guides.rubyonrails.org/getting_started.html,这并不表示控制器的变化是必要的。也就是说,尽管没有任何标签,新表单自动生成一个标签框。 – 2010-02-06 19:37:02

2

为了这个工作,你需要在正确的PARAMS散列通:

params = { 
    :link => { 
    :tags_attributes => [ 
     {:tag_one_attr => ...}, {:tag_two_attr => ...} 
    ], 
    :link_attr => ... 
    } 
} 

而且你的控制器看起来像:

def create 
    @link = Link.create(params[:link]) # this will automatically build the rest for your 
end 
0

尝试

<% f.fields_for :tags do |tag_form| %> 

(即失去_attributes在:tag_attributes)这就是我通常做嵌套表格

0

你需要建立一个标签在你的控制器或视图中的

def new 
    @link = @current_user.links.build 
    @link.tags.build 
end 

#in your view you can just use the association name 
<% f.fields_for :tags do |tag_form| %> 
7

看看你的代码

<% f.fields_for :tags_attributes do |tag_form| %> 

线需要使用的只是:tags代替:tags_attributes。 这将解决您的问题

请确保您有建立联系和标签在您的控制器像

def new 
    @link = @current_user.links.build 
    @link.tags.build 
end