2013-05-12 39 views
0

我似乎有通過我的連接表接收產品的問題,它給我一個奇怪的錯誤,因爲它似乎沒有收到我的訂單ID。我只能假設這是因爲訂單還沒有創建,但我在這一步創建訂單,所以訂單還沒有ID。所以這是我的問題。Rails:接收嵌套表單的問題有很多通過加入

這是我收到的錯誤:

ActiveRecord::RecordNotFound in OrdersController#create 

Couldn't find Product with ID=1 for Order with ID= 

Rails.root: /BillingSystem 
Application Trace | Framework Trace | Full Trace 

app/controllers/orders_controller.rb:10:in `new' 
app/controllers/orders_controller.rb:10:in `create' 

Request 

Parameters: 

{"utf8"=>"✓", 
"authenticity_token"=>"jE2wdERoxE7PKwBhN60KAfguxwAq8qdW4wbru51SMFg=", 
"order"=>{"client_id"=>"1", 
"products_attributes"=>{"1368396234677"=>{"id"=>"1", 
"_destroy"=>"false"}}}, 
"commit"=>"Create Order"} 

Show session dump 

Show env dump 
Response 

Headers: 

None 

新秩序觀:

<% if current_user %> 
    <div id="dashboard"> 
     <div id="logo"></div> 
     <table id="go_back_link_container"> 
      <tr> 
       <td> 
        <div class="go_back_link"> 
         <%= link_to "<- Go Back", "/orders/view" %> 
        </div> 
       </td> 
       <td> 
        <div id="user_display"> 
         Logged in as <%= current_user.email %>. 
         <%= link_to "Log out", log_out_path %> 
        </div> 
       </td> 
      </tr> 
     </table> 
     <%= form_for @order, method: :post do |f| %> 
      <% if @order.errors.any? %> 
       <div class="error_messages"> 
        <% for message in @order.errors.full_messages %> 
         * <%= message %> <br> 
        <% end %> 
       </div> 
      <% end %> 
      <p> 
       <%= f.label 'Select The Client' %><br /> 
       <%= select :order, :client_id, Client.all().collect { |c| [ (c.firstname + " " + c.surname), c.id ] } %> 
      </p> 

      <%= f.fields_for :products do |pf| %> 
       <% #render 'product_fields', f: builder %> 
      <% end %> 
      <%= link_to_add_fields "Add Product", f, :products %> 

      <p class="button"><%= f.submit %></p> 
     <% end %> 
     <% flash.each do |name, msg| %> 
      <%= content_tag :div, "* " + msg, :id => "flash_#{name}" %><br /> 
     <% end %> 
     <div id="copyright-notice"><div id="copyright_border">Copyright © Conner McCabe, all rights reserved.</div></div> 
    </div> 
<% else %> 
    <script type="text/javascript"> 
     window.location="<%= root_url %>" 
    </script> 
<% end %> 

訂貨型號:

class Order < ActiveRecord::Base 
    has_many :orderedproducts 
    has_many :products, through: :orderedproducts 
    has_one :client 

    attr_accessible :client_id, :order_total, :delivery_date, :products, :products_attributes 

    accepts_nested_attributes_for :products, :allow_destroy => true 

    before_save :generate_total 

    def generate_total 
     self.order_total = self.products.map(&:product_price).sum 
    end 
end 

訂單控制器:

class OrdersController < ApplicationController 
    def view 
     @orders = Order.all 
    end 

    def new 
     @order = Order.new 
    end 
    def create 
     @order = Order.new(params[:order]) 
     if @order.save 
      redirect_to '/orders/view', :notice => "Order Created!" 
     else 
      render "new" 
     end 
    end 
end 

產品領域的部分:

<fieldset> 
    <%= f.select :id, Product.all().collect {|p| [ p.product_name, p.id ] } %> 

    <%= f.hidden_field :_destroy %> 
    <%= link_to "remove", '#', class: "remove_fields" %> 
</fieldset> 

產品型號:

class Product < ActiveRecord::Base 
    #This line makes these elements accessible outside of the class. 
    attr_accessible :product_name, :product_price, :product_quantity, :product_supplier 

    has_many :orderedproducts 
    has_many :orders, through: :orderedproducts 

    #These attributes ensure that the data entered for each element is valid and present. 
    validates_presence_of :product_name 
    validates_presence_of :product_price 
    validates_numericality_of :product_price 
    validates_presence_of :product_quantity 
    validates_numericality_of :product_quantity 
    validates_presence_of :product_supplier 

end 

應用助手:

module ApplicationHelper 
    def link_to_add_fields(name, f, association) 
    new_object = f.object.send(association).klass.new 
    id = new_object.object_id 
    fields = f.fields_for(association, new_object, child_index: id) do |builder| 
     render(association.to_s.singularize + "_fields", f: builder) 
    end 
    link_to(name, '#', class: "add_fields", data: {id: id, fields: fields.gsub("\n", "")}) 
    end 
end 

有序產品型號:

class Orderedproduct < ActiveRecord::Base 
    attr_accessible :order_id, :product_id, :quantity_ordered 
    belongs_to :order 
    belongs_to :product 
end 

我公頃我列出了可能包含錯誤的所有可能的文件,我知道這有點過分,但它是與它有關的所有事情,並且更好地包含它而不是完全包含它。

我也遵循這個railscast指南:http://railscasts.com/episodes/196-nested-model-form-revised 爲了到達我所在的位置,我稍微編輯了它,以便它適合我的應用程序。

在此先感謝。

回答

1

我們對一個項目有類似的問題,除非關係是單數。問題是ActiveRecord正在尋找一個現有的關聯;像order.products.find(1)一樣。由於訂單是新記錄,所以這是行不通的。

您可以創建自己的products_attributes =方法並定義正確的行爲。但我認爲你可以爲連接模型(Orderedproduct)而不是Product使用嵌套屬性。

class Order 
    accepts_nested_attributes_for :orderedproducts 
end 

然後適當調整表單域。在新的形式

f.fields_for :products do |pf|變得f.fields_for :orderedproducts do |pf|

在局部

<%= f.select :id, Product.all().collect {|p| [ p.product_name, p.id ] } %>領域變得<%= f.select :product_id, Product.all().collect {|p| [ p.product_name, p.id ] } %>