[英]Rails 3.2.11 - create associated object on the fly
我有以下型號:
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 << @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? 謝謝。
問題出在您的jQuery中的這段代碼:document => @order.documents.build
。
當使用:document => @order.documents.build
調用局部: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}
建立關系。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.