ruby - passing html input from view to controller in rails -



ruby - passing html input from view to controller in rails -

my repo: https://github.com/gtar69/artstore_hw2 => it's shopping cart project

now want input value in carts/index.html alter cart_item quantity.

no thought how in rails

in typical php it's

<form action="backend.php"> <td><input type="number" name= "kobe"> <input type="submit" value="submit/> </td>

however, want simplify coding construction instead of using form.

my thought => view/carts/index.html

<tbody> <% current_cart.items.each |product| %> <tr> <td><%= render_product_photo(product.default_photo) %></td> <td> <%= link_to(product.title, admin_product_path(product)) %> </td> <td><%= product.price %></td> <td> <input type="number" name= "kobe" value = <%= cartitem.where(product_id: product.id).take!.count%>> <input type="submit" value="shala"> </td> <td><%= link_to("改變數量", change_item_quantity_carts_path(:product_id => product.id, :count => 2), :method => :post , :class => "btn btn-primary btn-lg btn-danger") %></td> <td><%= link_to("刪除物品", delete_item_carts_path(:product_id => product.id) , :method => :post, :class => "btn btn-primary btn-lg btn-danger") %></td> </tr> <% end %> </tbody>

passing "kobe" variable carts_controller

def change_item_quantity product = product.find(params[:product_id]) # :count => 2 current_cart.change_cart_item_quantity(product ,params[:kobe]) redirect_to carts_path end

and backend calculation in model/cart.rb

def change_cart_item_quantity(product, count) #改變cart_item中的數量 c= cartitem.where(product_id: product.id).take! c.count = count c.save end

i'm still struggling in how pass "kobe" controller. ideas?

thanks in advance.

refer guides.rubyonrails.org/form_helpers.html

i used

<td> <%= form_tag("/carts/change_item_quantity", method: "post") do%> <%= label_tag(:q, "改變數量") %> <%= number_field_tag(:kobe, cartitem.where(product_id: product.id).take!.count)%> <%= hidden_field_tag(:product_id, product.id)%> <%= submit_tag("change") %> <% end %> </td>

in way, can pass parameter needed controller.

ruby-on-rails ruby ruby-on-rails-3

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -