Rails ajax form for products -


i've been working app pizzeria customers order pizzas through website. working product page try submit products shopping cart through ajax, i'm stuck. haven't been able build shoppingcart accept product-id, product-size-id, extra-toppings array , quantity. decided try go session-store order-row ids stored , on menu page every product has form user add product, size , quantity shoppingcart keep getting error in server logs: started post "/order_row" ::1 @ 2015-08-03 11:18:21 +0300 processing orderrowscontroller#create 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 , session stores order-row-ids mentioned. menu page product_categories#show -view products belonging category 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 |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 |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 github-repo: https://github.com/casualcodeanddesign/ravintolamammamia

what's reason server error , why doesn't store order_row database?

activerecord::associationtypemismatch (product(#70158072501800) expected, got string(#70158039566200))

you need change

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

to

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

and product product_id in create.js.erb , order_rows_params


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -