Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在rails 3.2中创建和编辑嵌套对象?_Ruby On Rails_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 如何在rails 3.2中创建和编辑嵌套对象?

Ruby on rails 如何在rails 3.2中创建和编辑嵌套对象?,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,所以我有两个模型,报告和收据。每份报告都有许多收据。我使用脚手架生成我所有的视图和内容,但我正在改变,这样当用户创建新报告或编辑报告时,他们可以在表单中创建和编辑收据 我的模型设置为: class Report < ActiveRecord::Base has_many :receipts, :dependent => :destroy accepts_nested_attributes_for :receipts, :allow_destroy => true

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

我的模型设置为:

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
我的收据控制器

  # 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'

未定义的方法'receipts\u path'

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

要获取接受的嵌套属性,您需要:

  • 你的报告表格
  • 字段\u用于该形式的收据
  • 通过这种方式,您可以编辑给定报表的所有已创建收据。如果您还想创建新收据,可以添加一张空白收据:
    @report.receipts.build
    。您可以将此调用添加到
    新建
    编辑
    操作中

    请注意,您可以在报表的表单中编辑收据。这意味着您应该点击
    报告控制器
    ,而不是
    接收器控制器

    如果出现问题,这里有一些调试建议:

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

    编辑:我创建了一个包含所有相关文件的要点,以使嵌套表单正常工作:

    DoCheckout cocoon gem for nested resources表单。这个gem使得处理嵌套资源变得更加容易

    提供创建/更新操作的代码。在我看来,问题不在形式上。但是你的f.fields\u很奇怪。我会使用
    @report.receipts.build
    而不是
    receipts.new
    。但不要认为这会解决你的问题。添加了我的收据控制器。另外-@report.receipts.build实际上也给了我相同的路由错误…对于您的上一个问题(在(@receipts)
    表单上),您还需要传递
    报表
    form\u For[@report,@receipts]…
    ,因为它是一个嵌套资源(即路径包含报表id和收据id)也许浅的选项可以帮助你:2.7.2在真棒谢谢!给你50块赏金!快速问题,如何在表单中为每个收据添加删除链接?我尝试使用
    resources:receipts,:only=>[:destroy,:edit,:show]
    和指向
    :delete%>
    的链接为receipt添加嵌套资源,但我收到一个未定义的方法错误
    receipt\u path
    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
    
    <%= form_for(@receipt) do |receipt| %>
    
    undefined method `receipts_path'