1

我想使用单个更新按钮在单个视图中使用批量更新每个操作。使用以下代码,Rails 会抛出此错误

Showing /home/vincent/git/gestion/app/views/operations/tag.html.erb where line #23 raised:

undefined method `merge' for 1:Fixnum
Extracted source (around line #23):

20:         <td>
21:             <% @tags.each do |elem| %>
22:             <%= f.label elem.tag %>
23:             <%= f.check_box "operation[tag_ids][]", elem.id, operation.tags.include?(elem) %>
24:             <% end %>
25:         </td>
26:         <td><%= f.submit %></td>

楷模

class Operation < ActiveRecord::Base
  attr_accessible :credit, :date_operation, :debit, :libelle, :tag_ids
  has_and_belongs_to_many :tags
  accepts_nested_attributes_for :tags, :allow_destroy=>true
end

class Tag < ActiveRecord::Base
  attr_accessible :id, :tag
  has_and_belongs_to_many :operations
end

控制器

  def tag
    @operations = Operation.limit(100)
    @tags = Tag.all
    respond_to do |format|
      format.html { "tag" }# tag.html.erb
#      format.json { render json: @operations }
    end
  end

看法

<% @operations.each do |operation| %>
    <tr>

        <td><%= operation.date_operation %></td>
        <td><%= operation.libelle %></td>
        <td><%= operation.credit %></td>
        <td><%= operation.debit %></td>
        <%= form_for operation do |f| %>
        <td>
            <% @tags.each do |elem| %>
            <%= f.label elem.tag %>
            <%= f.check_box "operation[tag_ids][]", elem.id, operation.tags.include?(elem) %>
            <% end %>
        </td>
        <td><%= f.submit %></td>
        <% end %>
    </tr>
    <% end %>

你对这个问题有任何线索/帮助吗?

先感谢您

编辑 1:添加完整的堆栈跟踪

4

2 回答 2

0

您需要更改f.check_boxfor check_box_tag,例如:

<%= check_box_tag "operation[tag_ids][]", elem.id, operation.tags.include?(elem) %>

这种情况下的问题是f.check_box期望值绑定到在这种情况下不是形式的形式。

于 2013-03-18T19:24:37.877 回答
0

在这种情况下使用nested_form gem,我认为它会起作用。

有关nested_form 的更多信息,请单击此处

于 2013-03-18T23:24:03.343 回答