?Briella 2015-08-03 08:33 采纳率: 0%
浏览 21

Rails Ajax产品表单

I've been working with app for pizzeria where customers could order pizzas through their website. I currently working with the product page where I try to submit products to shopping cart through ajax, but I'm really stuck. I haven't been able to build a shoppingcart which would accept product-id, product-size-id, extra-toppings as an array and quantity. I decided to try to go with session-store where all the order-row ids are stored and on menu page every product has a form where user could add product, size and quantity to shoppingcart but I keep getting this error in server logs: Started POST "/order_row" for ::1 at 2015-08-03 11:18:21 +0300 Processing by OrderRowsController#create as JS Parameters: {"utf8"=>"✓", "order_row"=>{"product"=>"1", "size"=>"0", "quantity"=>"2"}, "commit"=>"Tilaa"} Completed 500 Internal Server Error in 2ms (ActiveRecord: 0.0ms)

ActiveRecord::AssociationTypeMismatch (Product(#70158072501800) expected, got String(#70158039566200)):
  app/controllers/order_rows_controller.rb:4:in `create'

I have models Product, ProductCategory, Order, OrderRow and my session stores order-row-ids as mentioned. My menu page is actually product_categories#show -view where products belonging to that category are listed.

#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-controller

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

And menu-page in 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 in order_rows#create action

 #create.js.erb
    $("#form-<%= params[:product] %>").load(document.URL + "#form-<%= params[:product]");

Associations:

#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 to github-repo: https://github.com/casualCodeAndDesign/ravintolamammamia

What's the reason for this server error and why it doesn't store my order_row to the database?

  • 写回答

1条回答 默认 最新

  • weixin_33709590 2015-08-05 15:04
    关注

    ActiveRecord::AssociationTypeMismatch (Product(#70158072501800) expected, got String(#70158039566200))

    You need to change

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

    to

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

    and product to product_id in create.js.erb and order_rows_params

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料