# Passing parameters to link\_to OR better way to do this?

**URL:** <https://www.ruby-forum.com/t/passing-parameters-to-link-to-or-better-way-to-do-this/50314>\
**Category:** Rails\
**Created:** [December 15, 2005, 2:15am UTC](https://www.ruby-forum.com/t/passing-parameters-to-link-to-or-better-way-to-do-this/50314 "2005-12-15T02:15:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Forrest\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ea5d25/32.png) [@Forrest\_C](https://www.ruby-forum.com/u/Forrest_C)\
**Post date:** [December 15, 2005, 2:15am UTC](https://www.ruby-forum.com/t/passing-parameters-to-link-to-or-better-way-to-do-this/50314/1 "2005-12-15T02:15:44Z")

</div>

Hi All:

I’m writing my 1st Rails app and I can’t seem to find the answer on  
the web or in the book.

I’m making a table, and I want to be able to expand a filename. The  
code is basically as as follows below. In the last

 entry, I want  
to call an action and pass in the test\_results\_path, which I will go  
and read a file and munge the data for a separate presentation. The  
code below shows me trying a variation of something I found, but I’m  
unable to get any parameters through to the action.:
# rhtml

```
  <% @logs.each do |log| %>
  <tr>
<td><%= log.base_name %></td>
<td><%= log.date_time %></td>
<td><%= log.status %></td>
<td><%= log.test_run_path %></td>
<td><%= link_to log.test_results_path, :action => "show_results",

```

# :id =\> o, :params =\> { :result\_file =\> log.test\_results\_path} %\>
  
  
\<% end %\>
# code:

def show\_results

# get the results file and do some munging

end

Is there a way to pass parameters to the link\_to(), OR is there some  
better way to do this in general that’s more Rails like?

Thanks

Forrest

---

<div class="post-metadata">

**Author:** ![Forrest\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ea5d25/32.png) [@Forrest\_C](https://www.ruby-forum.com/u/Forrest_C)\
**Post date:** [December 15, 2005, 9:25am UTC](https://www.ruby-forum.com/t/passing-parameters-to-link-to-or-better-way-to-do-this/50314/2 "2005-12-15T09:25:46Z")

</div>

Have a look at the API doc for link\_to [1]. The second parameter to  
link\_to is a hash of all parameters you want to send with the request.  
So the following should do what you want:

link\_to log.test\_results\_path, :action =\> “show\_results”, :id =\> o,  
:result\_file =\> log.test\_results\_path

As an aside, its probably a bad idea to include a filesystem path as a  
request parameter.

cheers,  
Gerret

[1]  
[http://api.rubyonrails.com/classes/ActionView/Helpers/UrlHelper.html#M000332](http://api.rubyonrails.com/classes/ActionView/Helpers/UrlHelper.html#M000332)

---

<div class="post-metadata">

**Author:** ![Forrest\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ea5d25/32.png) [@Forrest\_C](https://www.ruby-forum.com/u/Forrest_C)\
**Post date:** [December 15, 2005, 8:43pm UTC](https://www.ruby-forum.com/t/passing-parameters-to-link-to-or-better-way-to-do-this/50314/3 "2005-12-15T20:43:41Z")

</div>

why not just do:

link\_to log.test\_results\_path, :controller =\> “log”, :action =\>  
“show\_results”, :id =\> log

class LogController \< ApplicationController  
def show\_results  
log = Log.find(params[:id])  
if File.exist?(log.test\_results\_path)  
# do what you have to do to the file here  
end  
end  
end

or am i missing something?

---

<div class="post-metadata">

**Author:** ![Forrest\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ea5d25/32.png) [@Forrest\_C](https://www.ruby-forum.com/u/Forrest_C)\
**Post date:** [December 16, 2005, 12:16am UTC](https://www.ruby-forum.com/t/passing-parameters-to-link-to-or-better-way-to-do-this/50314/4 "2005-12-16T00:16:57Z")

</div>

On 12/15/05, Gerret A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Have a look at the API doc for link\_to [1]. The second parameter to  
> link\_to is a hash of all parameters you want to send with the request.  
> So the following should do what you want:
> 
> link\_to log.test\_results\_path, :action =\> “show\_results”, :id =\> o,  
> :result\_file =\> log.test\_results\_path

I swear that’s what I have in my .rhtml, but params[:result\_file] or  
params[:id] aren’t defined. Am I missing something?

Thanks

Forrest

---

<div class="post-metadata">

**Author:** ![Forrest\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ea5d25/32.png) [@Forrest\_C](https://www.ruby-forum.com/u/Forrest_C)\
**Post date:** [December 16, 2005, 12:39am UTC](https://www.ruby-forum.com/t/passing-parameters-to-link-to-or-better-way-to-do-this/50314/5 "2005-12-16T00:39:18Z")

</div>

Forrest,

You can pass parameters by adding them in the hash, like ‘:aparam’ below  
:

```
  <%= link_to "some action",
            {:controller => 'foos', :action => :new, :aparam => 

```

avalue } %\>

Alain

Note: I wrote ‘foos’ because :foos is not accepted (A bug).

---

<div class="post-metadata">

**Author:** ![Forrest\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ea5d25/32.png) [@Forrest\_C](https://www.ruby-forum.com/u/Forrest_C)\
**Post date:** [December 16, 2005, 8:22pm UTC](https://www.ruby-forum.com/t/passing-parameters-to-link-to-or-better-way-to-do-this/50314/6 "2005-12-16T20:22:32Z")

</div>

Thanks all.

I don’t know what mind altering substances I was under the influence  
wednesday, I swear I was doing what was suggested, but tried again fresh  
today and it worked.

Forrest

---

<div class="post-metadata">

**Author:** ![Forrest\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ea5d25/32.png) [@Forrest\_C](https://www.ruby-forum.com/u/Forrest_C)\
**Post date:** [December 16, 2005, 12:41am UTC](https://www.ruby-forum.com/t/passing-parameters-to-link-to-or-better-way-to-do-this/50314/7 "2005-12-16T00:41:07Z")

</div>

> I swear that’s what I have in my .rhtml, but params[:result\_file] or  
> params[:id] aren’t defined. Am I missing something?

OOPS sorry, it’s exactly what I had in a one of my attempts, but not  
in this one. I’ll muck around some more and see. I’m printing out  
the value of params, and no matter what I pass to to link\_to, I don’t  
see any other values in params.

Thanks

Forrest
