Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails 3 : How to create a path?

I need an explanation about how create new path on rails 3. I want this link for example

link_to "eat chocolate", eat_chocolate_user_path(user)

be equals to

link_to "eat chocolate", :controller => 'user', :action=>'eat_chocolate', :id=> user

I read many guides about it, including routing of railsguide. But i still do not know how it exactly work.

like image 790
perello Avatar asked Feb 02 '23 15:02

perello


2 Answers

add the following line to your routes.rb file.

match 'user/eat_chocolate/:id' => 'user#eat_chocolate', :as => :eat_chocolate_user

Look up named routes for more info.

like image 140
Kevin Tsoi Avatar answered Feb 05 '23 04:02

Kevin Tsoi


In some cases it will be better to use GET.

Using match will accept ALL http verbs GET PUT POST DELETE meaning that somebody could potentially mis-use your application. If all you're doing is showing something in a #show action, you should use this instead

get 'user/eat_chocolate/:id' => 'user#eat_chocolate', :as => :eat_chocolate_user

like image 27
Christian Flores Avatar answered Feb 05 '23 05:02

Christian Flores