28

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

协会:

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 控制器的 create 动作中抛出错误:

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

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

has_many => :through 案例中是否需要额外的步骤?这些似乎是基本 has_many 案例的唯一指示更改。

4

7 回答 7

9

看看你的代码行

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

您需要使用 just:tags而不是:tags_attributes. 这将解决您的问题

确保您的控制器中有构建链接和标签,例如

def new
  @link = @current_user.links.build
  @link.tags.build
end
于 2012-11-28T03:57:43.723 回答
6

我在stackoverflow上找到了这个:

带有has_many的Rails嵌套表单:通过,如何编辑连接模型的属性?

请告诉我它是否有效。

于 2011-08-04T00:08:10.880 回答
2

为了使它起作用,您需要传入正确的参数哈希:

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
于 2011-03-29T21:00:13.050 回答
1

尝试这个:

<% f.fields_for :tags_attributes do |tag_form| %>
于 2010-02-06T09:20:28.317 回答
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| %>
于 2011-09-02T08:20:14.637 回答
0

尝试

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

(即丢失 :tag_attributes 中的 _attributes)这就是我通常做嵌套表单的方式

于 2011-06-29T18:44:08.823 回答
0

在新操作的控制器中(加载表单部分),您是否通过链接构建@tag?

因此,您应该看到以下内容:

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

最好发布您的 links_controller 的 new 和 create 操作的内容。

于 2010-02-06T19:17:35.103 回答