Rails ajax 产品表格
Rails ajax form for products
我一直在为比萨店开发应用程序,客户可以通过他们的网站订购比萨饼。我目前正在处理产品页面,我尝试通过 ajax 将产品提交到购物车,但我真的卡住了。我无法构建一个购物车,它可以接受产品 ID、产品尺寸 ID、额外配料作为数组和数量。我决定尝试使用存储所有订单行 ID 的会话商店,并且在菜单页面上每个产品都有一个表单,用户可以在其中将产品、尺寸和数量添加到购物车,但我不断在服务器日志中收到此错误:
在 2015-08-03 11:18:21 +0300 为 ::1 开始 POST "/order_row"
由 OrderRowsController 处理#create as JS
参数:{"utf8"=>"✓", "order_row"=>{"product"=>"1", "size"=>"0", "quantity"= >"2"}, "commit"=>"Tilaa"}
在 2 毫秒内完成 500 个内部服务器错误(ActiveRecord:0.0 毫秒)
ActiveRecord::AssociationTypeMismatch (Product(#70158072501800) expected, got String(#70158039566200)):
app/controllers/order_rows_controller.rb:4:in `create'
我有模型 Product、ProductCategory、Order、OrderRow,我的会话存储订单行 ID,如前所述。我的菜单页面实际上是 product_categories#show -view,其中列出了属于该类别的产品。
#order_rows_controller.rb
class OrderRowsController < ApplicationController
respond_to :html, :js
def create
@orow = OrderRow.new(order_rows_params)
if @orow.save
session[:order_row_ids] << @orow.id
flash[:notice] = "Lisättiin ostoskoriin!"
else
flash[:error] = "Tuotteen lisääminen ostoskoriin epäonnistui."
redirect :back
end
end
def update
@orow = OrderRow.find(params[:id])
if @orow.update_attributes(params[:order_row])
flash[:notice] = "Ostoskori päivitetty."
else
flash[:error] = "Ostoskorin päivitys epäonnistui."
end
end
def destroy
@orow.find(params[:id]).destroy
flash[:notice] = "Tuote poistettu onnistuneesti"
end
private
def order_rows_params
params.require(:order_row).permit(:product, :size, :quantity) #, :extras => []
end
end
ProductCategories-控制器
class ProductCategoriesController < ApplicationController
before_action :set_product_category, only: [:edit, :update, :destroy]
respond_to :html, :js
def index
@product_categories = ProductCategory.all
end
def show
@product_category = ProductCategory.friendly.find(params[:id])
@product_categories = ProductCategory.all
@products = @product_category.products
@order_row = OrderRow.new(order: nil, product: nil, size: nil, extras: nil, quantity: nil)
end
和 product_categories/show 中的菜单页面。html.erb
#product_categories#show -view
<!--- category descriptions -->
<div class="container">
<% @products.each do |product| %>
<div class="col-sm-6 col-md-4">
<div class="product well">
<h3><%= product.name %></h3>
<span><%= product.description %></span>
<p class="prices">
<%= price(product.normal_price) %> | <%= price(product.plus_size_price) %> | <%= price(product.lunch_price) %>
</p>
<br>
<div id="form-<%= product.id %>">
<%= simple_form_for @order_row, :url => url_for(:controller => 'order_rows', :action => 'create'), remote: true do |f| %>
<%= f.hidden_field :product, :value => product.id %>
<h5>Koko</h5>
<div style="padding-left: 13px">
<%= f.input :size, collection: OrderRow.sizes, as: :radio_buttons, label: false, item_label_class: "radio-inline", item_wrapper_tag: false %>
</div>
<h5>Määrä</h5>
<div style="width: 8%; padding-left: 13px;">
<%= f.input :quantity, as: :string, label: false %>
</div>
<p>
<%= f.submit "Tilaa", class: "btn btn-success btn-lg" %>
</p>
<% end %>
</div>
</div>
</div>
<% end %>
</div>
Create.js.erb 在 order_rows#create 操作中
#create.js.erb
$("#form-<%= params[:product] %>").load(document.URL + "#form-<%= params[:product]");
协会:
#order_row
belongs_to :order
belongs_to :product
#product
belongs_to :product_category
has_one :campaign_producte
belongs_to :dish_type
#product_categories
has_many :products
has_many :campaign_products
has_many :product_extras
has_many :dish_types, through: :products
#product_extra
belongs_to :product_category
Link 到 github-回购:https://github.com/casualCodeAndDesign/ravintolamammamia
此服务器错误的原因是什么?为什么它没有将我的 order_row 存储到数据库中?
ActiveRecord::AssociationTypeMismatch (Product(#70158072501800)
expected, got String(#70158039566200))
你需要改变
<%= f.hidden_field :product, :value => product.id %>
到
<%= f.hidden_field :product_id, :value => product.id %>
and product
to product_id
in create.js.erb
and order_rows_params
我一直在为比萨店开发应用程序,客户可以通过他们的网站订购比萨饼。我目前正在处理产品页面,我尝试通过 ajax 将产品提交到购物车,但我真的卡住了。我无法构建一个购物车,它可以接受产品 ID、产品尺寸 ID、额外配料作为数组和数量。我决定尝试使用存储所有订单行 ID 的会话商店,并且在菜单页面上每个产品都有一个表单,用户可以在其中将产品、尺寸和数量添加到购物车,但我不断在服务器日志中收到此错误: 在 2015-08-03 11:18:21 +0300 为 ::1 开始 POST "/order_row" 由 OrderRowsController 处理#create as JS 参数:{"utf8"=>"✓", "order_row"=>{"product"=>"1", "size"=>"0", "quantity"= >"2"}, "commit"=>"Tilaa"} 在 2 毫秒内完成 500 个内部服务器错误(ActiveRecord:0.0 毫秒)
ActiveRecord::AssociationTypeMismatch (Product(#70158072501800) expected, got String(#70158039566200)):
app/controllers/order_rows_controller.rb:4:in `create'
我有模型 Product、ProductCategory、Order、OrderRow,我的会话存储订单行 ID,如前所述。我的菜单页面实际上是 product_categories#show -view,其中列出了属于该类别的产品。
#order_rows_controller.rb
class OrderRowsController < ApplicationController
respond_to :html, :js
def create
@orow = OrderRow.new(order_rows_params)
if @orow.save
session[:order_row_ids] << @orow.id
flash[:notice] = "Lisättiin ostoskoriin!"
else
flash[:error] = "Tuotteen lisääminen ostoskoriin epäonnistui."
redirect :back
end
end
def update
@orow = OrderRow.find(params[:id])
if @orow.update_attributes(params[:order_row])
flash[:notice] = "Ostoskori päivitetty."
else
flash[:error] = "Ostoskorin päivitys epäonnistui."
end
end
def destroy
@orow.find(params[:id]).destroy
flash[:notice] = "Tuote poistettu onnistuneesti"
end
private
def order_rows_params
params.require(:order_row).permit(:product, :size, :quantity) #, :extras => []
end
end
ProductCategories-控制器
class ProductCategoriesController < ApplicationController
before_action :set_product_category, only: [:edit, :update, :destroy]
respond_to :html, :js
def index
@product_categories = ProductCategory.all
end
def show
@product_category = ProductCategory.friendly.find(params[:id])
@product_categories = ProductCategory.all
@products = @product_category.products
@order_row = OrderRow.new(order: nil, product: nil, size: nil, extras: nil, quantity: nil)
end
和 product_categories/show 中的菜单页面。html.erb
#product_categories#show -view
<!--- category descriptions -->
<div class="container">
<% @products.each do |product| %>
<div class="col-sm-6 col-md-4">
<div class="product well">
<h3><%= product.name %></h3>
<span><%= product.description %></span>
<p class="prices">
<%= price(product.normal_price) %> | <%= price(product.plus_size_price) %> | <%= price(product.lunch_price) %>
</p>
<br>
<div id="form-<%= product.id %>">
<%= simple_form_for @order_row, :url => url_for(:controller => 'order_rows', :action => 'create'), remote: true do |f| %>
<%= f.hidden_field :product, :value => product.id %>
<h5>Koko</h5>
<div style="padding-left: 13px">
<%= f.input :size, collection: OrderRow.sizes, as: :radio_buttons, label: false, item_label_class: "radio-inline", item_wrapper_tag: false %>
</div>
<h5>Määrä</h5>
<div style="width: 8%; padding-left: 13px;">
<%= f.input :quantity, as: :string, label: false %>
</div>
<p>
<%= f.submit "Tilaa", class: "btn btn-success btn-lg" %>
</p>
<% end %>
</div>
</div>
</div>
<% end %>
</div>
Create.js.erb 在 order_rows#create 操作中
#create.js.erb
$("#form-<%= params[:product] %>").load(document.URL + "#form-<%= params[:product]");
协会:
#order_row
belongs_to :order
belongs_to :product
#product
belongs_to :product_category
has_one :campaign_producte
belongs_to :dish_type
#product_categories
has_many :products
has_many :campaign_products
has_many :product_extras
has_many :dish_types, through: :products
#product_extra
belongs_to :product_category
Link 到 github-回购:https://github.com/casualCodeAndDesign/ravintolamammamia
此服务器错误的原因是什么?为什么它没有将我的 order_row 存储到数据库中?
ActiveRecord::AssociationTypeMismatch (Product(#70158072501800) expected, got String(#70158039566200))
你需要改变
<%= f.hidden_field :product, :value => product.id %>
到
<%= f.hidden_field :product_id, :value => product.id %>
and product
to product_id
in create.js.erb
and order_rows_params