【问题标题】:Rails 3.2.11 - create associated object on the flyRails 3.2.11 - 动态创建关联对象
【发布时间】:2013-02-13 11:53:37
【问题描述】:

我有以下型号:

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

【问题讨论】:

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


    【解决方案1】:

    问题在于 jQuery 中的这段代码::document =&gt; @order.documents.build。

    当您使用:document =&gt; @order.documents.build 调用部分时,您传递的是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 =&gt; true} 的情况下建立关系。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多