Rails 3.2.11 - 动态创建关联对象

时间:2013-02-13 11:53:38

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

我有以下型号:

Document.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

Order.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? 谢谢。

1 个答案:

答案 0 :(得分:1)

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

当您使用:document => @order.documents.build调用partial时,您正在传入Document对象的实例,而您真正要传入的内容是ActionView::Helpers::FormBuilder个对象。

您可能需要查看this answer,它可能会帮助您。

另外,在我看来,OrdersController

中存在逻辑错误

不应该这样:

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

是这样的:

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

因为我认为你只想在传递{:add_document => true}的情况下建立关系。