gpt4 book ai didi

mysql - rails : Issue with recieving nested forms with has many through join

转载 作者:行者123 更新时间:2023-11-29 13:52:11 24 4
gpt4 key购买 nike

我似乎在通过连接表接收产品时遇到问题,这给了我一个奇怪的错误,因为它似乎没有收到我的订单的 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

新订单 View :

<% 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

订单 Controller :

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为了达到我现在的目的,我对其进行了稍微编辑,以使其适合我的应用程序。

提前致谢。

最佳答案

我们在一个项目中遇到了类似的问题,只不过关系是单一的。问题是 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 ] } %>

关于mysql - rails : Issue with recieving nested forms with has many through join,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16512588/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com