【问题标题】:How to create and and edit nested objects in rails 3.2?如何在 Rails 3.2 中创建和编辑嵌套对象?
【发布时间】:2012-12-29 02:26:04
【问题描述】:

所以我有两个模型,报告和收据。每份报告都有许多收据。我使用脚手架来生成我所有的视图和东西,但我改变了一些东西,以便当用户创建新报告或编辑报告时,他们可以在表单中创建和编辑收据。

我的模型已经设置好了:

class Report < ActiveRecord::Base

    has_many :receipts, :dependent => :destroy
  accepts_nested_attributes_for :receipts, :allow_destroy => true

    attr_protected :id

end
class Receipt < ActiveRecord::Base
    belongs_to :report

    attr_protected :id

    validates_presence_of :vendor, :date, :description, :amount, :acctCode
end

我已设置表单以创建新收据:

    <%= form_for @report do |f| %>
     ....
       <%= f.fields_for :receipts, Receipt.new do |receipt| %>
       ...
       <% end %>
    <% end %>

但是每次我去保存报告时,都会出现路由错误:

No route matches {:action=>"edit", :controller=>"receipts", :report_id=>#<Receipt id: nil, date: nil, vendor: "", description: "", amount: nil, companyCard: false, lobbyingExpense: false, acctCode: "", created_at: nil, updated_at: nil, report_id: 2>}

我的路线设置为:

resources :reports do
    resources :receipts
end

我的收据控制器有

  # GET /receipts/new
  def new
    @receipt = Receipt.new

    respond_to do |format|
      format.html # new.html.erb
    end
  end

  # GET /receipts/1/edit
  def edit
    @receipt = Receipt.find(params[:id])
  end

  # POST /receipts
  def create
    @receipt = Receipt.new(params[:receipt])

    respond_to do |format|
      if @receipt.save
        format.html { redirect_to @receipt.Report, notice: 'Receipt was successfully created.' }
      else
        format.html { render action: "new" }
      end
    end
  end

我有一段时间没碰过铁轨了,所以我不知道我做错了什么。但是在我的旧应用程序 (3.1) 中,当我添加图像来表示博客文章时,我什至没有图像控制器,除了通过 ajax 删除它们。我在这里有一个收据控制器的唯一原因是因为我使用脚手架来生成视图等。

编辑 - 我还应该指出,如果我转到新的收据视图,我会在表单标签上收到错误:

<%= form_for(@receipt) do |receipt| %>

undefined method `receipts_path'

【问题讨论】:

  • 提供创建/更新操作的代码。在我看来,问题不在形式上。但是你的 f.fields_for 很奇怪。
  • 我会使用@report.receipts.build 而不是Receipt.new。但不要认为这会解决你的问题。
  • 添加了我的收据控制器。还有 - @report.receipts.build 实际上给了我同样的路由错误......
  • 对于你的最后一个问题(在form_for(@receipt)),你还需要传递Report:form_for [@report, @receipt] ...,因为它是一个嵌套资源(即路径包含报告的id和收据ID)
  • 也许浅选项可以帮助你:edgeguides.rubyonrails.org/routing.html#nested-resources中的 2.7.2

标签: ruby-on-rails ruby-on-rails-3


【解决方案1】:

如果您使用accepts_nested_attributes_for,则不需要额外的控制器来管理记录。当然,如果您需要特定页面,例如收据的“显示视图”,则需要该控制器。

要获得accepts_nested_attributes_for,您需要:

  1. 报告表格
  2. 以该形式使用fields_for :receipts

这样,您可以编辑给定报告的所有已创建收据。如果您还想创建新收据,可以添加空白收据:@report.receipts.build。您可以将此调用添加到您的 new 和 edit 操作中。

请注意,您在报表的表单中编辑收据。这意味着,您应该点击 ReportsController 而不是 ReceiptsController。

如果事情在这里不起作用,请提供一些调试建议:

  • 执行 rake 路由以查看是否所有内容都已正确定义。
  • 检查从form_for(@report) 生成的HTML。尤其是表单标签的 'action=""' 属性是相关的。它应该指向“/reports/X”

编辑:我创建了一个包含所有相关文件的 Gist 以使嵌套表单正常工作:https://gist.github.com/4420280

【讨论】:

  • 非常感谢! 50个赏金给你!快速提问,我如何在每张收据的表格中包含删除链接?我尝试使用resources :receipts, :only =&gt;[:destroy, :edit, :show] 和link_to &lt;%#= link_to 'Delete', receipt_path(@report, receipt.object.id), :method =&gt; :delete %&gt; 添加用于接收的嵌套资源,但我得到一个未定义的方法错误receipt_path
【解决方案2】:

为嵌套资源表单检查茧宝石。这个 gem 使处理嵌套资源的工作变得更加容易。 https://github.com/nathanvda/cocoon

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-26
    • 2010-10-25
    • 2019-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多