具有has_many =>的accepts_nested_attributes_for :通过选项 [英] accepts_nested_attributes_for with has_many => :through Options

查看:72
本文介绍了具有has_many =>的accepts_nested_attributes_for :通过选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型,链接和标签,通过第三个模型link_tags关联.下面的代码在我的Link模型中.

I have two models, links and tags, associated through a third, link_tags. The following code is in my Link model.

协会:

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操作:

links_controller Actions:

  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查看代码:

View code from 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控制器中的create动作中引发错误:

The following line of code, in the create action in the Link controller throws an error:

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

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

has_many =>是否需要其他步骤:通过案例?对于基本的has_many情况,这些似乎是唯一表明的更改.

Are there additional steps needed in the has_many => :through case? These seem to be the only indicated changes for the basic has_many case.

推荐答案

看看代码行

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

您只需要使用:tags而不是:tags_attributes. 这样可以解决您的问题

You need to use just :tags instead of :tags_attributes. This will solve your issue

确保您在控制器中拥有

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

这篇关于具有has_many =&gt;的accepts_nested_attributes_for :通过选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆