My repo: https://github.com/Gtar69/artstore_hw2 => It's my shopping cart project
Now I want to input a value in the carts/index.html to change cart_item quantity.
No idea how to do that in rails
In typical php it's like
<form action="backend.php">
<td><input type="number" name= "kobe">
<input type="submit" value="submit/>
</td>
However, I want simplify the coding structure instead of using form.
My idea is => view/carts/index.html
<tbody>
<% current_cart.items.each do |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 to 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 do 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 to pass "kobe" to controller. Any ideas?
Thanks in advance.
refer to 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 that way, I can pass any parameter I needed to controller.
Related
Basically I created a controller, model and view for subjects. Basically I have 6 actions inside my controller and set up a REST inside my routes file to route the right file.
When I entered, http://localhost:3000/subjects/index it shows me the view for show.html.erb instead of the index.html.erb
Here's what my subject controller looks like:
class SubjectsController < ApplicationController
def index
#subjects = Subject.sorted
end
And here's the content of my index.html.erb file.
<% #page_title = "All Subjects" %>
<div class="subjects index">
<h2>Subjects</h2>
<%= link_to("Add New Subject", new_subject_path, :class => "action_new") %>
<table class="listing" summary="Subject list" border="1">
<tr class="header">
<th>#</th>
<th>Subject</th>
<th>Visible</th>
<th>Pages</th>
<th>Actions</th>
</tr>
<% #subjects.each do |subject| %>
<tr>
<td><%= subject.position %> </td>
<td><%= subject.name %> </td>
<td class="center"><%= status_tag(subject.visible) %></td>
<td class="center"><%= subject.pages.size %> </td>
<td class="actions">
<%= link_to("View Pages", pages_path(:subject_id => subject.id), :class => 'action show') %>
<%= link_to("Show", subject_path(subject), :class => 'action show') %>
<%= link_to("Edit", edit_subject_path(subject), :class => 'action edit') %>
<%= link_to("Delete", delete_subject_path(subject), :class => 'action delete') %>
<td>
</tr>
<% end %>
<table>
</div>
Also here's what I set up on my routes:
resources :subjects do
member do
get :delete
end
end
Any idea what am I missing?
The answer is simple: in order you access the index page, you need to hit the following URL:
http://localhost:3000/subjects
Obviously, it will be with GET
The reason why you got the error is: since, anything of the format subjects/:id will take you to the show action within SubjectsController, so Rails interprets subjects/index as you are trying to access the page subjects/:id with index as id of the subject. Here's nothing wrong with Rails, since in RESTful web services, you access the index page by ONLY using the plural name of the resource, like in your case http://localhost:3000/subjects
Now, Im doing shopping cart project and met some questions that I don't know
how to pass value from view to model in ruby on rails.
all coding is here => https://github.com/Gtar69/artstore_hw2
My idea is that I want to input quantity in the view/carts/index.html
and pass quantity vars to model/carts.rb to do some simple calculation to render
total_price in the cart... but I don't have idea how to do that!
Very Thanks !!!
in view/carts/index.html
<tbody>
<% current_cart.items.each do |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= product.id value="1"/></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>
</table>
<div class="total group">
<span class="pull-right">
<span> 總計 <%= render_cart_total_price(current_cart) %> NTD
</span>
</div>
<div class = "checkout">
<%= link_to("刪除全部", delete_all_carts_path,:method => :post, :class => "btn btn-primary btn-lg btn-danger pull-left") %>
</div>
<div class="checkout">
<%= link_to("確認結賬", "#" , :method => :post , :class => "btn btn-primary btn-lg btn-danger pull-right") %>
</div>
in carts.rb
class Cart < ActiveRecord::Base
has_many :cart_items, :dependent => :destroy
has_many :items, :through => :cart_items, :source => :product
def add_product_to_cart(product)
items << product
# cart_items和product
end
def remove_product_from_cart(product)
items.destroy(product)
#cart_items.where(:product_id => product.id).destroy_all
end
def remove_all_products_in_cart
items.destroy_all
#clear => destory_all
end
def total_price
items.inject(0){|sum, item| sum +item.price}
end
end
in carts_helper
module CartsHelper
def cart_items_count(cart)
cart.cart_items.count
end
def render_cart_total_price(current_cart)
current_cart.total_price
end
end
Firstly, I wouldn't suggest using the function in the helper.
Secondly, in your view:
<div class="total group">
<span class="pull-right">
<span> 總計 <%= current_cart.total_price %> NTD
</span>
Your model:
def total_price
self.items.pluck(:price).inject(:+)
end
Pluck will return an array from the price column and the inject adds all the elements in the array.
Hope that works!
I'm trying to edit a collection of users. I want to render table with a list of names and checkboxes. I'm close, but I'm missing something.
My form looks like so:
<%= form_for 'users[]', :url => approve_users_path, :html => {:class => 'form-horizontal'} do |f| %>
<tbody>
<% for user in #users %>
<%= fields_for user do |u| %>
<tr>
<td><%= user.name %></td>
<td><%= u.check_box :vouched %></td>
</tr>
<% end %>
<% end %>
</tbody>
<% end %>
which generates
<tr>
<td>steve cabillero</td>
<td><input name="user[vouched]" type="hidden" value="0" /><input id="user_vouched" name="user[vouched]" type="checkbox" value="1" /></td>
however, I need the input name in the form of users[id][vouched] and vouched is a virtual attribute.
when I try to use f.check_box I get a method not found error.
you should use form_tag if you are not using a specific resource in the form. I also suggest using #each instead of a for loop and check_box_tag for the checkbox
<%= form_tag approve_users_path, :class => 'form-horizontal' do %>
<tbody>
<% #users.each do |user| %>
<tr>
<td><%= user.name %></td>
<td><%= check_box_tag "users[#{user.id}][vouched]" %></td>
</tr>
<% end %>
</tbody>
<% end %>
I have a form with a store number (collection select box), date (3 select boxes), and a list of items with a quantity field beside each item. I have been able to pass the items and quantities as an array of hashes, but I can't get the date and store numbers in because I can't seem to change their name to include the [].
Here's the code
<% form_for(#credit) do |f| %>
<% stores = Store.find(:all, :conditions => { :active => true }, :order => :storeNum) %>
Store Number<%= f.collection_select :storeNum, stores, :storeNum, :storeNum %>
<br /><%= f.date_select :credDate %>
<table>
<tr>
<th>Quanity</th>
<th>Item Number</th>
<th>Name</th>
</tr>
<% #items.each do |item| %>
<tr>
<td>
<%= f.text_field ( :quantity, :name => "credit[][quantity]", :size => 3 ) %>
<%= f.hidden_field :item, :name => "credit[][itemNum]", :value => item.itemNum %>
</td>
<td><%=h item.itemNum %></td>
<td><%=h item.name %></td>
</tr>
<% end %>
<tr><td colspan="3">
<%= f.submit 'Submit' %></td>
</tr>
</table>
<% end %>
Any help is surely appreciated. I've been working on this two days.
Have you considered simple HTML inputs?
<input type="text" name="credit[][quantity]" size="3" id="quantity"></input>
How do I close a form_tag? Here's my code:
<%= form_tag :action => 'authenticate' %>
<h1>Already a member?</h1>
<table>
<tr>
<td>Username*: </td>
<td><%= text_field("userform", "user_name", :size => "20", :class => "field") %></td>
</tr>
<tr>
<td>Password*: </td>
<td><%= password_field("userform", "password", :size => "20", :class => "field") %></td>
</tr>
<tr>
<td></td><td><input type="submit" value="Login" class="form_button" /></td>
</tr>
</table>
<hr />
<%= form_tag :action => 'register' %>
<h1>Register</h1>
<table>
<tr>
<td>Username*: </td>
<td><%= text_field("userform", "user_name", :size => "20", :class => "field") %></td>
</tr>
<tr>
<td>Password*: </td>
<td><%= password_field("userform", "password", :size => "20", :class => "field") %></td>
</tr>
<tr>
<td>Email*: </td>
<td><%= text_field("userform", "password", :size => "20", :class => "field") %></td>
</tr>
<tr>
<td></td><td><input type="submit" value="Register" class="form_button" /></td>
</tr>
</table>
I tried <% end %> and <% end_form_tag %>, but I got errors. (Unexpected kEND). I've Googled around a bit, and nothing I've seen really helps. Oh, if I delete everything after the horizontal ruler, the form works fine. But I'd like to have two forms on the page...
I'm using Rails 2.3.5.
form_tag also takes a block, inside which you can put the form elements, whereupon it will be closed automatically. From the docs:
<% form_tag '/posts' do -%>
<div><%= submit_tag 'Save' %></div>
<% end -%>
# => <form action="/posts" method="post"><div><input type="submit" name="submit" value="Save" /></div></form>
Short version (see also: http://dev.rubyonrails.org/ticket/7391):
</form>
?
Correct version:
<% form_tag '/someform' do -%>
<div><%= submit_tag 'Submit' %></div>
<% end -%>
My way to fix it is to include the submit button right before the end and make the button not display.
<%= submit_tag('',style: 'width:0;height:0;display:none;') %>
<% end %>
This puts the ending tag right where I need it.
My fix is placing the form tag just above the table tag.
<%= form_tag(update_password_path, :method=> "post") do |f| %>
<p id="notice"><%= notice %></p>
<table align="center">