So I have the classes: Products, Carts, and LineItems – standard relationship (products and harts have many line items, and vice versa) and I one of the properties of the line items is size. When you show the Product in more detail, there is a form asking you to select the size, the form looks like this (there are arbitrary values in the select box)
<%= form_for(:line_item, :url => {:controller => "line_items", :action => 'create', product_id: @product.id }) do |f| %>
<ul>
<li>Size: <%= f.select(:size, 0..99) %></li>
<li><%= link_to 'Add to Cart', line_items_path(product_id: @product.id), :method => :post %></li> <!-- This comes up later in the question -->
<li><%= submit_tag "Add to Cart" %></li> <!-- using this Add to Cart button for now -->
</ul>
<% end %>
My first question is to resolve a problem. In the create definition of line_items I have the following two lines, and the problem is is that regardless of the size chosen, it records it as 1. Even if I choose 0, even if I choose 99. So what is my flaw in logic here? I’m sure I’m making a pretty big mistake, I just don’t know enough of what I’m doing wrong to properly search for the answer
@temp_size = params[:line_item]
@line_item.size = @temp_size.size
And my second question is that I would like to have a link as opposed to a button – I would prefer to be able to do this with a link like the 4th line in my form (middle list item) – are there properties I could use to replace the submit button?
Thanks!
First question:
params[:line_item]is a hash containing data from the input fields of the submitted form.@temp_size.sizeis callingHash#sizewhich returns a count of key-value pairs in the hash. Since your form has only one input field,:size,@temp_size.sizewill always return 1 regardless of it’s value.To assign the value of the
:sizeinput field, do this:Second question:
You can use CSS to change the appearance of the submit button. You could also use a Javascript click handler to submit the form when a link is clicked. This should be it’s own question.