Forrest Chang
2005-Dec-15 01:14 UTC
passing parameters to link_to OR better way to do this?
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
Gerret Apelt
2005-Dec-15 08:24 UTC
Re: passing parameters to link_to OR better way to do this?
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 On 12/15/05, Forrest Chang <fkchang2000-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Chris Hall
2005-Dec-15 19:42 UTC
Re: passing parameters to link_to OR better way to do this?
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? On 12/15/05, Gerret Apelt <gerret.apelt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 > > 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 > > > On 12/15/05, Forrest Chang <fkchang2000-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > 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 > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Forrest Chang
2005-Dec-15 23:15 UTC
Re: passing parameters to link_to OR better way to do this?
On 12/15/05, Gerret Apelt <gerret.apelt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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
Forrest Chang
2005-Dec-15 23:19 UTC
Re: passing parameters to link_to OR better way to do this?
> 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
Alain Ravet
2005-Dec-15 23:39 UTC
Re: passing parameters to link_to OR better way to do this?
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). -- Posted via http://www.ruby-forum.com/.
Forrest Chang
2005-Dec-16 19:21 UTC
Re: Re: passing parameters to link_to OR better way to do this?
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 On 12/15/05, Alain Ravet <alainravet-spam2004-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:> > 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). > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails