bbqInKoloa
2006-Apr-01 22:58 UTC
[Rails] Quick question about <% link_to something, :action => ...
Hi, I have a basic question. when i use <% link_to show_city, :action => ''my defined action'' :id => ''something'' %> //what is ID here? is this the parameter in my mysql db? basically how can i put a parameter into the link that when it hits my function, it can tell my function what to use as a condition to search. for example i have a table of cities that i want to all list as links. when the user clicks the link of a city, i want to generate a new page with all information related to the city link the person pressed. -- Posted via http://www.ruby-forum.com/.
Adam Bloom
2006-Apr-02 04:45 UTC
[Rails] Re: Quick question about <% link_to something, :action => ..
bbqInKoloa wrote:> Hi, > I have a basic question. when i use > > <% link_to show_city, > :action => ''my defined action'' > :id => ''something'' %> > > //what is ID here? is this the parameter in my mysql db?It''s a parameter that will be sent to the linked page, and can be accesed with: params[:id] in ''my defined action.'' You can make up your own, for example: <% link_to ''Show'', :action => ''show'', :id => ''something'', :type => ''city'' %> and you could access the string ''city'' with params[:type].> basically > > how can i put a parameter into the link that when it hits my function, > it can tell my function what to use as a condition to search. for > example i have a table of cities that i want to all list as links. when > the user clicks the link of a city, i want to generate a new page with > all information related to the city link the person pressed.Assuming your table looks something like this: <table> <% for city in Cities.find_all %> <td><% link_to city.name, :action => ''show_city'', :id => city %></td> <% end %> This will create a table of links, which will be named according to their city, and will link to the action "show_city" in your controller. In your controller code you''ll put a line something like this: def show_city @city = Cities.find(params[:id]) ... end and in your file "show_city.rhtml", you''ll put stuff like: <td>@city.name</td> <td>@city.state</td> Hope that helps. -Adam -- Posted via http://www.ruby-forum.com/.
Adam Bloom
2006-Apr-02 04:46 UTC
[Rails] Re: Quick question about <% link_to something, :action => ..
Oh, I should mention that if it does in fact say: id => ''something'' the string ''something'' is what will be accessed by params[:id] In my cases, id will be an item from your database. -Adam -- Posted via http://www.ruby-forum.com/.
bbqInKoloa
2006-Apr-02 14:47 UTC
[Rails] Re: Quick question about <% link_to something, :action => ..
hey adam, thanks so much for the response! appreciate the help. Adam Bloom wrote:> Oh, I should mention that if it does in fact say: > > id => ''something'' > > the string ''something'' is what will be accessed by > > params[:id] > > In my cases, id will be an item from your database. > > -Adam-- Posted via http://www.ruby-forum.com/.