1

I have controller and form but when I try to submit my form it does not submit values to controller.

Form:

= form_for @cart do |f|
  .overflow
    - unless @product.equipment_payments.blank?
      | Equipment Payment Option
      br
      = f.select :equipment_payments, options_for_select(@product.equipment_payments), {:prompt => 'Price'}
    br
    br
    - if @product.product_plan_durations.count > 0
      - plan_durations = PlanDuration.where(id: @product.product_plan_durations.pluck(:plan_duration_id))
      | Product Plan Duration
      br
      = f.select :product_plan_duration_id, options_from_collection_for_select(plan_durations, :id, :name), {:prompt => 'Plan Duration'}
    br
    br
    .modal-footer.form-group
      = f.submit "Add to Cart"

CartsController

class CartsController < ApplicationController

  before_action :load_product

  def new
    @cart = Cart.new
  end

  def create
    @cart = Cart.new(cart_params)
    @cart.save
  end

  private

  def cart_params
    params.require(:cart).permit(:equipment_payments, :product_plan_duration_id)
  end

  def load_product
    @product = Product.find(params[:product_id])
  end

end
1:04:40 AM web.1 |  Started POST "/carts" for ::1 at 2019-12-09 01:04:40 +0500
1:04:40 AM web.1 |  Processing by CartsController#create as HTML
1:04:40 AM web.1 |    Parameters: {"utf8"=>"✓", "authenticity_token"=>"c9TcwAMtRp6MyYbgxeAcrjcklHeIFxwFmX7vhgEB5Cig8iikCE9ycSmEgfmwLEIL6myLWpQiqV5MS9WN6iMkrw==", "cart"=>{"equipment_payments"=>"Lease", "product_plan_duration_id"=>"4"}, "commit"=>"Add to Cart"}
1:04:40 AM web.1 |    User Load (0.8ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 36], ["LIMIT", 1]]
1:04:40 AM web.1 |  Redirected to http://localhost:3000/product_search?utf8=%E2%9C%93&product_search_form%5Bc%5D=3&product_search_form%5Bsc%5D=105&product_search_form%5Bq%5D=&commit=Search
1:04:40 AM web.1 |  Completed 302 Found in 7ms (ActiveRecord: 0.8ms)
1:04:40 AM web.1 |  Started GET "/product_search?utf8=%E2%9C%93&product_search_form%5Bc%5D=3&product_search_form%5Bsc%5D=105&product_search_form%5Bq%5D=&commit=Search" for ::1 at 2019-12-09 01:04:40 +0500
1:04:40 AM web.1 |  Processing by ProductSearchController#index as HTML
1:04:40 AM web.1 |    Parameters: {"utf8"=>"✓", "product_search_form"=>{"c"=>"3", "sc"=>"105", "q"=>""}, "commit"=>"Search"}

routes.rb

resources :carts

Link from which I am loading my form:

 = link_to 'Add to Cart', new_cart_path(product_id: product.id), remote: true

The problem is it just submit the data but its not hitting in create method of Carts controller.

LearningROR
  • 211
  • 1
  • 13
  • You say it's not "hitting" the create method, but I see this: `Processing by CartsController#create as HTML`. What does the rest of the transaction log look like? Perhaps there is a `rollback` because a `Cart` is supposed to `belong_to` a `User` or something along that line? – jvillian Dec 08 '19 at 20:30
  • @jvillian My Cart model is empty. – LearningROR Dec 08 '19 at 20:35
  • I don't know what "my Cart model is empty" means. Can you please show your entire transaction log from your server? Showing the two lines you provide is not enough. – jvillian Dec 08 '19 at 20:37
  • Have a look please. – LearningROR Dec 08 '19 at 20:43
  • can you use save with a bang like save! in ur creat method? I feel you get more info regarding to your issues. if u do, please post it up – Kick Buttowski Dec 08 '19 at 20:46
  • 1
    @KickButtowski I tried but that didn't work as expected. Let me post then. – LearningROR Dec 08 '19 at 20:49
  • take a look at this https://stackoverflow.com/questions/1761076/when-do-i-use-save-create-and-update-attributes-in-rails – Kick Buttowski Dec 08 '19 at 20:49
  • Basically I have a link from which I click and load form in modal. Then after submission it should submit at desired path which its not submiting. – LearningROR Dec 08 '19 at 20:51
  • did u get any different error message when u used save!? – Kick Buttowski Dec 08 '19 at 20:52
  • No I am not getting any error. It only do thing like this: `rescue_from ActiveRecord::RecordNotFound, with: :record_not_found`. Its my client project so I am not sure how he is working with it. – LearningROR Dec 08 '19 at 20:54
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/203859/discussion-between-kick-buttowski-and-learningror). – Kick Buttowski Dec 08 '19 at 20:54

1 Answers1

1

1. The create action of the controller is being hit:

You have this logs:

"1:04:40 AM web.1 | Started POST "/carts" for ::1 at 2019-12-09 01:04:40 +0500" which suggests that you are hitting the controller.

2. You have a before action: load_product - but no product ID is being passed! (I don't see it being passed with the params hash shown in the logs)

You will either have to add a hidden field in your form, with the product ID in there, or you can skip that before filter entirely.

before_action :load_product, only: [:create]

3. Then apply a redirect

def create
    @cart = Cart.new(cart_params)
    @cart.save
    redirect_to root_path  ## add the line
end

Hopefully that should work.

Note: This is quick and dirty The above is a quick and dirty solution to just make it work. Perhaps you will want a better solution: i.e. if the cart doesn't save, then how will you handle that? Or perhaps you may indeed want to load the product when you are saving a cart? How will you handle the situation if someone manually adds a different product id via the hidden field? You might not want to redirect to the root path if everything saves. In other words, consider what you want, and perhaps make the code better to suit your purposes.

BenKoshy
  • 33,477
  • 14
  • 111
  • 80