[Rails] Re: Quick question about <% link_to something, :action => ..

Adam Bloom admanb at gmail.com
Sun Apr 2 04:44:59 GMT 2006


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/.


More information about the Rails mailing list