ruby on rails - link_to object, how do I define the path? -
let's run command
rails g scaffold movie title:string desc:text in generated index-file, /app/views/movies/index.html.erb, movie-objects looped this:
<% @movies.each |movie| %> <tr> <td><%= movie.title %></td> <td><%= link_to 'show', movie %></td> # , other columns </tr> <% end %> i don't understand link_to 'show', movie part. in routing-file, movie-objects using resourceful routing ( resources :movies ), couldn't find out how works.
tl;dr: if create link link_to "some place", theobject, lead , how can (re)define in routing?
to explain, when use link_to "...", object, have remember ruby object-orientated, rails expects object have relevant data contained inside (if set correctly).
objects
so if you're defining @movies = movie.all, you're getting activerecord object containing collection of movie objects inside. lay man, appear array of data objects, can cycle through using .each loop in example:
<%= @movies.each |movie| %> ... <% end %> this code therefore output each object @movies collection - giving rails details needs build link_to path
--
routing
if you're routing using resources :model helper, rails create series of restful routes you:
get /photos photos#index display list of photos /photos/new photos#new return html form creating new photo post /photos photos#create create new photo /photos/:id photos#show display specific photo /photos/:id/edit photos#edit return html form editing photo patch/put /photos/:id photos#update update specific photo delete /photos/:id photos#destroy delete specific photo this how link_to helper know route use passing object method
hope helps
Comments
Post a Comment