Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql Rails:接收具有多个直通连接的嵌套表单时出现问题_Mysql_Ruby On Rails_Ruby_Has Many Through - Fatal编程技术网

Mysql Rails:接收具有多个直通连接的嵌套表单时出现问题

Mysql Rails:接收具有多个直通连接的嵌套表单时出现问题,mysql,ruby-on-rails,ruby,has-many-through,Mysql,Ruby On Rails,Ruby,Has Many Through,我似乎在通过我的联接表接收产品时遇到问题,它给了我一个奇怪的错误,因为它似乎没有收到我订单的ID。我只能假设这是因为订单尚未创建,但我在这一步中创建了订单,因此订单还没有ID。这就是我的问题 以下是我收到的错误: ActiveRecord::RecordNotFound in OrdersController#create Couldn't find Product with ID=1 for Order with ID= Rails.root: /BillingSystem Applica

我似乎在通过我的联接表接收产品时遇到问题,它给了我一个奇怪的错误,因为它似乎没有收到我订单的ID。我只能假设这是因为订单尚未创建,但我在这一步中创建了订单,因此订单还没有ID。这就是我的问题

以下是我收到的错误:

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 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
class Orderedproduct < ActiveRecord::Base
  attr_accessible :order_id, :product_id, :quantity_ordered
  belongs_to :order
  belongs_to :product
end
class Orderedproduct
我列出了每一个可能包含错误的文件,我知道这有点过分,但这是与它有关的所有事情,最好我包含它,而不是完全不包含它

我还遵循了railscast指南: 为了了解我的情况,我对它进行了轻微的编辑,使其适合我的应用程序


提前谢谢。

我们在一个项目中遇到了类似的问题,只是关系是单一的。问题是ActiveRecord正在寻找一个现有的关联;类似于order.products.find(1)。因为订单是新记录,所以这不起作用

您可以创建自己的产品\u attributes=method并定义正确的行为。但我认为您可以只为联接模型(Orderedproduct)使用嵌套属性,而不是产品

class Order
  accepts_nested_attributes_for :orderedproducts
end
然后适当地调整表单字段。以新的形式

f.fields_for:products do | pf |
变为
f.fields_for:ordered products do | pf |

在田野里

变成

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
class Order
  accepts_nested_attributes_for :orderedproducts
end