1

我有以下型号:

文档.rb

class Document < ActiveRecord::Base

  belongs_to :order
  attr_accessible :docfile, :print_format, :user_comment, :paper_type
  has_attached_file :docfile,
                    :styles => {
                      :thumb => "100x100>"
                    },
                    :url => "/order_documents/:order_number/:style/:id.:extension",
                    :path => ":rails_root/public/order_documents/:order_number/:style/:id.:extension"

  validates_attachment_size :docfile, :less_than => 100.megabytes
  validates_attachment_content_type :docfile,
                                    :content_type => ['image/jpg', 'image/jpeg', 'image/png', 'application/zip', 'application/x-zip']

end

订单.rb

class Order < ActiveRecord::Base

  belongs_to :user
  has_many :documents, :dependent => :destroy
  accepts_nested_attributes_for :documents, :allow_destroy => true

  DELIVERY_COMMENT_ROWS_SIZE = 2
  DELIVERY_COMMENT_COLS_SIZE = 40

  validates_associated :documents
  validates :delivery_street, :delivery_address, :presence => true

end

我有Orders_controller.rb:

class OrdersController < ApplicationController

  def new
    @order = Order.new
    unless params[:add_document]
      @order.documents.build
    end
    respond_to do |format|
      format.html
      format.js
    end
  end

  def create
    @order = Order.new(params[:order])
    current_user.orders &lt;&lt; @order
    respond_to do |wants|
      if @order.save
        flash[:notice] = 'Заказ создан успешно.'
        wants.html {redirect_to my_orders_path}
        wants.xml { render :xml =&gt @order.to_xml }
      else
        wants.html { render :action => "new" }
        wants.xml {render :xml =&gt @order.errors}
      end
    end
  end

end

new.erb.html用于“新”操作:

<%= form_for @order, :url => orders_path, :html => { :multipart => true } do |order| %>
  <%= order.fields_for :documents do |document| %>
    <%= render :partial => "add_document", :locals => {:document => document} %>
  <% end %>

  <div id="documents"></div>
  <%= link_to "add document...", new_order_path(:add_document => true), remote: true %>
  <%= submit_tag 'save order', :class => 'submit' %>
<% end %>

和部分_add_document.erb:

 <%= document.file_field :docfile %>
 <%= document.select(:print_format, Document::PRINT_FORMAT) %>
 <%= document.select(:paper_type, Document::PAPER_TYPE) %>
 <%= document.text_area :user_comment, :rows => Document::USER_COMMENT_ROWS_SIZE, :cols => Document::USER_COMMENT_COLS_SIZE %>

我在动态创建新文档时遇到问题(使用 JQuery)。我有new.js.erb:

$('<%= escape_javascript(render :partial => "add_document", :locals => { :document => @order.documents.build }) %>').appendTo($('#documents'));

但得到错误:

ActionView::Template::Error (undefined method `file_field' for #<Document:0x695c3d0>)

请帮忙。我必须将什么 jquery 代码写入文件 new.js.erb?谢谢你。

4

1 回答 1

1

问题在于 jQuery: 中的这段代码:document => @order.documents.build

当您调用 partial 时,:document => @order.documents.build您传递的是Document对象的一个​​实例,而您真正想要传递的是一个ActionView::Helpers::FormBuilder对象。

您可能想查看此答案,它可能会对您有所帮助。

另外,在我看来,您的 : 中有一个逻辑错误OrdersController

这不应该:

unless params[:add_document]
  @order.documents.build
end

变成这样:

if params[:add_document]
  @order.documents.build
end

因为我认为您只想在{:add_document => true}传入时建立关系。

于 2013-02-13T16:39:08.123 回答