【问题标题】:Rails ajax form for products产品的Rails ajax表单
【发布时间】:2015-08-03 08:33:23
【问题描述】:

我一直在为比萨店开发应用程序,客户可以通过他们的网站订购比萨饼。我目前正在使用产品页面,我尝试通过 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"= >“提拉”} 在 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 和我的会话存储 order-row-ids 如上所述。我的菜单页面实际上是 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>

在 order_rows#create action 中创建.js.erb

 #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

github-repo 链接:https://github.com/casualCodeAndDesign/ravintolamammamia

此服务器错误的原因是什么?为什么它没有将我的 order_row 存储到数据库中?

【问题讨论】:

  • 你能发布你的关联代码吗?
  • 我在文章末尾添加了它们:)!
  • 尝试将这一行 &lt;%= f.hidden_field :product, :value =&gt; product.id %&gt; 更改为 &lt;%= f.hidden_field :product_id, :value =&gt; product.id %&gt; 并在 create.js.erborder_rows_params 中将 product 更改为 product_id
  • 成功了,谢谢:)!!
  • 太棒了!我将其添加为答案:)

标签: ruby-on-rails ajax session shopping-cart


【解决方案1】:

ActiveRecord::AssociationTypeMismatch (Product(#70158072501800) 预期,得到字符串(#70158039566200))

你需要改变

<%= f.hidden_field :product, :value => product.id %> 

<%= f.hidden_field :product_id, :value => product.id %> 

productproduct_idcreate.js.erborder_rows_params

【讨论】:

    猜你喜欢
    • 2013-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多