In the first controller, I set my session variables:
def show
@item = Item.find(params[:id])
session[:item_id] = @item.id
session[:amount] = params[:amount]
end
My view sets the amount with a form_tag
:
<%= form_tag checkout_transaction_path, method: :get do %>
<%= label_tag :amount %>
<%= text_field :amount, placeholder: "Total bid amount", autofocus: true %>
<%= submit_tag "submit" %>
<% end %>
The parameters that get sent with this form look like this:
Parameters: {"utf8"=>"✓", "amount"=>{"{:placeholder=>\"Total bid amount\", :autofocus=>true}"=>"1111"}, "commit"=>"Confirm offer", "id"=>"1"}
The second controller tries to assign the session variable to an instance variable.
def checkout
@item = session[:item]
@amount = session[:amount]
end
However, only @item
is working. I try to multiply @amount * 0.10
but get this error: undefined method '*' for nil:NilClass
What causes that error? The submit params says it's being submitted, but maybe something is up with the way I try to retrieve it? session[:item_id]
goes through perfectly.
You have issue here:
Parameters: {"utf8"=>"✓", "amount"=>{"{:placeholder=>\"Total bid amount\", :autofocus=>true}"=>"1111"}, "commit"=>"Confirm offer", "id"=>"sam-lipp-abandonment"}
In your parameters, you send amount parameters but in amount, you send HTML form "amount"=>{"{:placeholder=>\"Total bid amount\", :autofocus=>true}"=>"1111"}
, but should send only value what you set in the amount form, and your amount parameters should looks like this example! "amount"=>"1"
So this means that your form doesn't work correctly! Try please replace your form on this one, and in a controller, you will get amount!
<%= form_for @new_item, url: checkout_transaction_path do |f| %>
<%= f.label :amount %>
<%= f.text_field :amount, placeholder: "Total bid amount", autofocus: true %>
<%= submit_tag "submit" %>
<% end %>
OR the same code for form_tag
<%= form_tag checkout_transaction_path do %>
<%= label_tag :amount %>
<%= text_field_tag :amount, placeholder: "Total bid amount", autofocus: true %>
<%= submit_tag "Submit Post" %>
<% end %>
The problem in your form can be here:
you wrote text_field
instead text_field_tag
Also for form_for
form, you need to add in the controller in a method with variable what you will use in your form, for example, I'm using variable @new_item
where from you call this form, something like this
def new
@new_item = Item.new
end