首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails 3:批量更新

Rails 3:批量更新
EN

Stack Overflow用户
提问于 2013-03-18 04:08:10
回答 2查看 328关注 0票数 1

我想在一个视图中使用批量更新每个操作,只有一个更新按钮。使用以下代码,Rails将处理此错误

代码语言:javascript
复制
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>

模型

代码语言:javascript
复制
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

控制器

代码语言:javascript
复制
  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

视图

代码语言:javascript
复制
<% @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:添加完整堆栈跟踪

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-03-19 03:24:38

您需要更改check_box_tagf.check_box,例如:

代码语言:javascript
复制
<%= check_box_tag "operation[tag_ids][]", elem.id, operation.tags.include?(elem) %>

这个场景中的问题是,f.check_box期望该值被绑定到本例中不是这样的形式。

票数 0
EN

Stack Overflow用户

发布于 2013-03-19 07:24:03

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15465596

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档