ruby on rails - Adding Products to a Cart -


see update's 1 & 2 below solution

i've been following tutorial on creating shopping cart book "agile web development rails"..they have chapter i've been following here: http://media.pragprog.com/titles/rails4/cart.pdf

in book have create line_item get's passed cart , stored in session using line <%= button_to 'add cart', line_items_path(product_id: product) %> in case changed <%= button_to '', line_items_path(gear_id: gear), id: 'rent_it' %>

if want change it's form accepts other parameters how this?

below code i've used , attempted accomplish far unsuccessfully. below code , error can't find gear without 'id'. i'm new rails , programming in general appreciate help.

update 1 (i updated view below well)

i can see server logs i'm passing id of gear in form. trying do.

processing lineitemscontroller#create html   parameters: {"utf8"=>"✓", "authenticity_token"=>"0qnjshabov+avpx0ajhxummskfwtwflkpma6cbhtu3s=", "line_item"=>{"rentstart"=>"", "rentend"=>"", "gear_id"=>"13"}, "commit"=>""}   cart load (0.4ms)  select `carts`.* `carts` `carts`.`id` = 18 limit 1 completed 500 internal server error in 2ms  activerecord::recordnotfound (couldn't find gear without id):   app/controllers/line_items_controller.rb:44:in `create' 

i'm still getting error couldn't find gear without id. 2 lines below:

    gear = gear.find(params[:gear_id])     @line_item = @cart.add_gear(gear.id) 

what missing?

line item's controller

  def create     @cart = current_cart     gear = gear.find(params[:gear_id])     @line_item = @cart.add_gear(gear.id)      respond_to |format|       if @line_item.save         format.html { redirect_to @line_item.cart }         format.json { render json: @line_item, status: :created, location: @line_item }       else         format.html { render action: "new" }         format.json { render json: @line_item.errors, status: :unprocessable_entity }       end     end   end 

cart model

class cart < activerecord::base   has_many :line_items, dependent: :destroy    def add_gear(gear_id)     current_item = line_items.find_by_gear_id(gear_id)     if current_item       current_item.quantity += 1     else       current_item = line_items.build(params[:line_item])     end     current_item   end    def total_price     line_items.to_a.sum { |item| item.total_price }   end  end 

view gear show page

<div class="gearside_date_main">         <h3>rental date</h3>         <script>         $(function() {             var dates = $( "#rentstart, #rentend" ).datepicker({                 defaultdate: "+1w",                 changemonth: true,                 numberofmonths: 1,                 onselect: function( selecteddate ) {                     var option = this.id == "rentstart" ? "mindate" : "maxdate",                         instance = $( ).data( "datepicker" ),                         date = $.datepicker.parsedate(                             instance.settings.dateformat ||                             $.datepicker._defaults.dateformat,                             selecteddate, instance.settings );                     dates.not( ).datepicker( "option", option, date );                 }             });         });         </script>       <%= form_for lineitem.new |f| %>         <%= f.text_field :rentstart, id: 'rentstart'  %>         <%= f.text_field :rentend, id: 'rentend'  %>         <%= f.hidden_field :gear_id, :value => @gear.id %>         <%= f.submit "", id: 'rent_it' %>     <% end %>         <% end %>     </div> 

update 2

i got working after murifox's comments. didn't understand how syntax structured when passing hash. after trial , error working. i'm including updated code below see changes made. know when see answers i'm looking change. hope helps else....

the view (gear show page

<%= form_for lineitem.new |f| %>     <%= f.text_field :rentstart, id: 'rentstart'  %> <%= f.text_field :rentend, id: 'rentend'  %> <%= f.hidden_field :gear_id, :value => @gear.id %>     <%= f.submit "", id: 'rent_it' %> <% end %> 

the controller (create action)

  def create     @cart = current_cart     gear = gear.find(params[:line_item][:gear_id])     lineitem = params[:line_item]     @line_item = @cart.add_gear(lineitem, gear.id)      respond_to |format|       if @line_item.save         format.html { redirect_to @line_item.cart }         format.json { render json: @line_item, status: :created, location: @line_item }       else         format.html { render action: "new" }         format.json { render json: @line_item.errors, status: :unprocessable_entity }       end     end   end 

the cart model

  def add_gear(lineitem, gear_id)     current_item = line_items.find_by_gear_id(gear_id)     if current_item       current_item.quantity += 1     else       current_item = line_items.build(lineitem)     end     current_item   end 

try using

gear = gear.find(params[:line_item][:gear_id]) 

Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -