Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic path helpers rails

Tags:

What are the paths that is automatically added by Rails? Let say you have a Question resource you automatically get questions_path, question_path etc. Where do I see what they resolve to and what I get?

like image 272
LuckyLuke Avatar asked Aug 08 '12 11:08

LuckyLuke


1 Answers

This section might be helpful http://guides.rubyonrails.org/routing.html#specifying-a-controller-to-use

Verb    Path              Action     Helper  GET     /photos           index      photos_path GET     /photos/new       new        new_photo_path POST    /photos           create     photos_path GET     /photos/:id       show       photo_path(:id) GET     /photos/:id/edit  edit       edit_photo_path(:id) PUT     /photos/:id       update     photo_path(:id) DELETE  /photos/:id       destroy    photo_path(:id) 

If you want to create a helper for show action you can write

photo_path(@photo.id) 

where @photo is your model object. Or you can pass @photo directly if it responds to id method.

photo_path(@photo) edit_photo_path(@photo) 

You can also load rails console (in terminal) and test routes using app like so app.photo_path(1) (it will show you the route for the photo with id equals 1)

like image 53
evfwcqcg Avatar answered Sep 20 '22 05:09

evfwcqcg