Forum: Ruby on Rails passing parameters to link_to OR better way to do this?

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.
37a786a2425b62259869a86adf5219bb?d=identicon&s=25 Forrest Chang (Guest)
on 2005-12-15 02:15
(Received via mailing list)
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 <td> 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} %> </td>
      </tr>
      <% 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
5ceea097a3b29cb6a5da6705926410f4?d=identicon&s=25 Gerret Apelt (Guest)
on 2005-12-15 09:25
(Received via mailing list)
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/Help...
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (Guest)
on 2005-12-15 20:43
(Received via mailing list)
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?
37a786a2425b62259869a86adf5219bb?d=identicon&s=25 Forrest Chang (Guest)
on 2005-12-16 00:16
(Received via mailing list)
On 12/15/05, Gerret Apelt <gerret.apelt@gmail.com> 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
B45eab4f18aa1bb2a44d6e657531a642?d=identicon&s=25 Alain Ravet (aravet)
on 2005-12-16 00:39
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).
37a786a2425b62259869a86adf5219bb?d=identicon&s=25 Forrest Chang (Guest)
on 2005-12-16 00:41
(Received via mailing list)
>  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
37a786a2425b62259869a86adf5219bb?d=identicon&s=25 Forrest Chang (Guest)
on 2005-12-16 20:22
(Received via mailing list)
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
This topic is locked and can not be replied to.