Forum: Ruby on Rails Quick question about <% link_to something, :action => ...

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
bbqInKoloa (Guest)
on 2006-04-02 02:58
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.
Adam B. (Guest)
on 2006-04-02 08:44
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
Adam B. (Guest)
on 2006-04-02 08:46
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
bbqInKoloa (Guest)
on 2006-04-02 18:47
hey adam, thanks so much for the response! appreciate the help.


Adam B. 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
This topic is locked and can not be replied to.