Passing parameters to link_to OR better way to do this?


#1

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


#2

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


#3

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?


#4

On 12/15/05, Gerret A. 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


#5

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


#6

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


#7

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