Default procedure in controller_path

6 views
Skip to first unread message

PC Leung

unread,
Nov 13, 2013, 9:18:19 AM11/13/13
to hk...@googlegroups.com

Hi All


I do not understand how come the "line_items_path" trigger to call the procedure 'create' in line_items_controller.rb  ? There is no 'create' within 'button_to' construct.

many thanks in advance


% cache ['store', Product.latest] do %> <% @products.each do |product| %>

<% cache ['entry', product] do %> <div class="entry">

<%= image_tag(product.image_url) %> <h3><%= product.title %></h3>
<%= sanitize(product.description) %> <div class="price_line">

<span class="price"><%= number_to_currency(product.price) %></span>

<%= button_to 'Add to Cart', line_items_path(product_id: product) %> </div>

</div> <% end %>

<% end %> <% end %> 

Francis Chong

unread,
Nov 13, 2013, 10:49:32 AM11/13/13
to hk...@googlegroups.com
Hi

Rails route use Restful convention here. 

Rails router consider two parts when routing your request: the path and the HTTP method.

The button_to take the path parameter, it have a :method parameter, which is default :post (mapped to create).

Check the rails router guide should help you understand this better: http://guides.rubyonrails.org/routing.html

I also recommend Rails 4 Way to get started: https://leanpub.com/tr4w

Cheers
Francis

--
You received this message because you are subscribed to the Google Groups "Hong Kong Ruby on Rails" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hkror+un...@googlegroups.com.
To post to this group, send email to hk...@googlegroups.com.
Visit this group at http://groups.google.com/group/hkror.
For more options, visit https://groups.google.com/groups/opt_out.

PC Leung

unread,
Nov 13, 2013, 9:44:14 PM11/13/13
to hk...@googlegroups.com
Thanks Francis

Paul Gresham

unread,
Nov 13, 2013, 10:01:31 PM11/13/13
to hk...@googlegroups.com
If I remember correctly

line_items_path()   will give you "/line_tems" which rails routing points you to index
line_items_path(LineItem.new) will give you the link to create
line_items_path(LineItem.find(1)) will link you to show for that particular line item
line_item_edit_path(LineItem.find(1)) will generate the path for editing the item

Obviously a LineItem with an ID of 1 will have to exist.


Reply all
Reply to author
Forward
0 new messages