我有以下型号:
文档.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 << @order
respond_to do |wants|
if @order.save
flash[:notice] = 'Заказ создан успешно.'
wants.html {redirect_to my_orders_path}
wants.xml { render :xml => @order.to_xml }
else
wants.html { render :action => "new" }
wants.xml {render :xml => @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?谢谢你。