Hello all, I''ve just started using Rails and Ruby and have been amazed at how fast I can get a basic app up and running. I hate to ask this questions since it seems so simple but I can''t find any help in the archives. I want to add a parameter to the end of a URL using link_to but have been unable to find a way to do this. It''s possible that Rails offers a more elegant solution but what I''m trying to do is list a set of "products" that belong to a "company" So from the "company" list page, I have a link "Show Products" ... so I need to pass the company_id in order to create the "company" controller and reference it''s collection of products ... right? So my link_to URL currently looks like this /products/list. This produces a list of ALL products. I want to add ?for_company=2. Doesn''t look so elegant as written so I''m thinking there is a better approach. What I have found is that producing the following URL works /products/list/2 where"2" is the company id and this is handled properly in the controller unless I want to list all products ... I would like to better understand how to add parameters to the URL or the more elegant approach to the problem I''m posing. Thanks for the support, Thomas _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
"Thomas W. Shelton" <twshelton-hjxG0kmX7RhDPfheJLI6IQ@public.gmane.org> writes:> I want to add a parameter to the end of a URL using link_to but have > been unable to find a way to do this.<%= link_to "Blah blah", :controller => "somethings", :action => "doit", :param1 => "value1", :param2 => "value2", :etc => "you get the idea" %> -- Doug Alcorn - http://lathi.net/RubyOnRailsDeveloper doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org
On Oct 13, 2005, at 8:45 AM, Doug Alcorn wrote:> "Thomas W. Shelton" <twshelton-hjxG0kmX7RhDPfheJLI6IQ@public.gmane.org> writes: > >> I want to add a parameter to the end of a URL using link_to but have >> been unable to find a way to do this. > > <%= link_to "Blah blah", :controller => "somethings", :action => > "doit", :param1 => "value1", :param2 => "value2", :etc => "you get the > idea" %>This will work, but isn''t particularly the "Rails Way". Take a look at config/routes.rb. -- -- Tom Mornini
If company is an object in,say, @company, you could more simply do: <%= link_to "Products", :controller => "products", :action => "show", :id => @company %> This should create the pretty URL: /products/show/2 , where 2 is the primary key (id) for company -- no routes required.
Tom Mornini <tmornini-W/9V78bTXriB+jHODAdFcQ@public.gmane.org> writes:> On Oct 13, 2005, at 8:45 AM, Doug Alcorn wrote: > >> "Thomas W. Shelton" <twshelton-hjxG0kmX7RhDPfheJLI6IQ@public.gmane.org> writes: >> >>> I want to add a parameter to the end of a URL using link_to but have >>> been unable to find a way to do this. >> >> <%= link_to "Blah blah", :controller => "somethings", :action => >> "doit", :param1 => "value1", :param2 => "value2", :etc => "you get the >> idea" %> > > This will work, but isn''t particularly the "Rails Way". > > Take a look at config/routes.rb.I agree that a named route would be better. However, that''s the simplest answer to a simple question. http://wiki.rubyonrails.org/rails/pages/NamedRoutes -- Doug Alcorn - http://lathi.net/RubyOnRailsDeveloper doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org
Thomas W. Shelton wrote:> What I have found is that producing the following URL works > > /products/list/2 where"2" is the company id and this is handled properly > in the controller unless I want to list all products ...I think that what you''re doing is heading the right way. So now you can generate lists for individual companies, but if you want to generate a list of all companies products you could do something like this in your controller: if params[:id] @products = Product.find(:all, :conditions => ["company_id ?",params[:id]]) else @products = Product.find(:all) end Sean
Here''s how I''d approach this - I had a similar problem recently. It probably makes sense to add a new method to your "products" controller, to show all products by a company. (This is assuming you have a Product model and a Company model and Company has_many :products). So first put something like this in your product_controller.rb : def bycompany @products = Company.find(:all, :conditions => ["company_id = ?", @params[:company_id]]) end Then create a route in routes.rb much like this: map.connect ''products/bycompany/:company_id'', :controller => "products", :action => "bycompany" This means that a url of products/bycompany/2 will pass all products of the company with company_id "2" as a big fat instance array to your template, and then you can list them all. Rather than appending query strings, the "more elegant approach" (which I''ve taken so far) is to work out the URL you''d like to use, work out if it can be created with the controllers and methods you have, and then write a route for it. Once you''ve got a route sorted, you can then created the necessary methods on the appropriate controllers. It''s just a case of working backwards. It''s pretty much what everyone else has said, but I thought I''d try to help :) t. On 13/10/05, Thomas W. Shelton <twshelton-hjxG0kmX7RhDPfheJLI6IQ@public.gmane.org> wrote:> > Hello all, > > I''ve just started using Rails and Ruby and have been amazed at how fast I > can get a basic app up and running. > > I hate to ask this questions since it seems so simple but I can''t find any > help in the archives. I want to add a parameter to the end of a URL using > link_to but have been unable to find a way to do this. It''s possible that > Rails offers a more elegant solution but what I''m trying to do is list a set > of "products" that belong to a "company" > > So from the "company" list page, I have a link "Show Products" ... so I need > to pass the company_id in order to create the "company" controller and > reference it''s collection of products ... right? So my link_to URL > currently looks like this /products/list. This produces a list of ALL > products. I want to add > ?for_company=2. Doesn''t look so elegant as written so I''m thinking there is > a better approach. > > What I have found is that producing the following URL works > > /products/list/2 where"2" is the company id and this is handled properly in > the controller unless I want to list all products ... > > I would like to better understand how to add parameters to the URL or the > more elegant approach to the problem I''m posing. > > Thanks for the support, > Thomas > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >