Rick R
2010-Sep-07 22:46 UTC
Stuck on something that should be so darn simple - link_to a "foo_bar" controller method?
Man, as a newbie as soon as I deviate just an inch from the hello world stuff I''m in all sorts of hurt:)... I have a controller (lets call it GroceryStoreController) that has an ''add'' method (It can be foobar for all I care at the moment, just something that isn''t the conventional ones produced from scaffolding and that you get mappings to automatically from ''resources'' definition in Routes.) I now have a list of grocery store items and I want to provide an ''add'' link next to each item. Hitting the add link should reach the "add" method in the controller (passing along the id of the grocery item.) This should be so simple, yet I can''t seem to get anything to work. I don''t think I can just use my own xyz_path in link_to since I think that''s just a convention for the built in resources (edit ,etc.) eg: link_to ''add'', add_grocery_store_path(item) #causes undefined path error I even tried doing things a way that docs say was and older way?: //sorry pasting from real code I was working with (results after you do a search)... not grocery_store, but same idea... @meters.each do |m| .... link_to ''add'', controller => "meters_search", :action => "add", :id => m The above creates a link but it looks really bad: <a href="/meters_search/add?%23%3CMetersSearchController%3A0x00000102e90c48%3E=meters_search&id=1">add</a> and when you click on it it''s trying to find the ''show'' method (which I don''t have in this controller, for now just have index, and add until I get this working) so it breaks. Maybe it''s my Routes that is a problem, and I''ve been trying different things there as well. Relevant routes for this example I have as: resources :meters_search get "meters_search/add" => ''meters_search#add'' [ side not for any help in my learning: I''m also am trying to following the docs for link_to http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to and it''s extremely confusing. It shows some options but then also mentions see options available by looking at "url_for" so when I click on that link http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-url_for I see seven options listed on the page ( anchor, only_path, etc.) yet they keep showing examples of options like :action, :controller yet I don''t see them shown anywhere in the API as valid options? ] -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Philip Hallstrom
2010-Sep-07 22:54 UTC
Re: Stuck on something that should be so darn simple - link_to a "foo_bar" controller method?
On Sep 7, 2010, at 3:46 PM, Rick R wrote:> Man, as a newbie as soon as I deviate just an inch from the hello > world stuff I''m in all sorts of hurt:)... > > I have a controller (lets call it GroceryStoreController) that has an > ''add'' method (It can be foobar for all I care at the moment, just > something that isn''t the conventional ones produced from scaffolding > and that you get mappings to automatically from ''resources'' definition > in Routes.) > > I now have a list of grocery store items and I want to provide an > ''add'' link next to each item. Hitting the add link should reach the > "add" method in the controller (passing along the id of the grocery > item.) > > This should be so simple, yet I can''t seem to get anything to work. I > don''t think I can just use my own xyz_path in link_to since I think > that''s just a convention for the built in resources (edit ,etc.) eg:Look into the "member" option... map.resource :grocery_stores, :member => {:add => :get} -philip> link_to ''add'', add_grocery_store_path(item) #causes undefined path error > > I even tried doing things a way that docs say was and older way?: > > //sorry pasting from real code I was working with (results after you > do a search)... not grocery_store, but same idea... > > @meters.each do |m| > .... > link_to ''add'', controller => "meters_search", :action => "add", :id => m > > The above creates a link but it looks really bad: > > <a href="/meters_search/add?%23%3CMetersSearchController%3A0x00000102e90c48%3E=meters_search&id=1">add</a> > > and when you click on it it''s trying to find the ''show'' method (which > I don''t have in this controller, for now just have index, and add > until I get this working) so it breaks. > > Maybe it''s my Routes that is a problem, and I''ve been trying different > things there as well. Relevant routes for this example I have as: > > resources :meters_search > get "meters_search/add" => ''meters_search#add'' > > > [ side not for any help in my learning: > I''m also am trying to following the docs for link_to > http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to > and it''s extremely confusing. It shows some options but then also > mentions see options available by looking at "url_for" so when I click > on that link http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-url_for > I see seven options listed on the page ( anchor, only_path, etc.) yet > they keep showing examples of options like :action, :controller yet I > don''t see them shown anywhere in the API as valid options? ] > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Rick R
2010-Sep-07 23:49 UTC
Re: Stuck on something that should be so darn simple - link_to a "foo_bar" controller method?
On Tue, Sep 7, 2010 at 6:54 PM, Philip Hallstrom <philip-LSG90OXdqQE@public.gmane.org> wrote:>> This should be so simple, yet I can''t seem to get anything to work. I >> don''t think I can just use my own xyz_path in link_to since I think >> that''s just a convention for the built in resources (edit ,etc.) eg: > > Look into the "member" option... > > map.resource :grocery_stores, :member => {:add => :get} > > -philipThanks Philip! That was the key. After I added: resources :meters_search do member do get ''add'' end end I was able to use link_to ''add'', add_meters_search_path(m) -- Rick -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Phillip
2010-Sep-08 01:08 UTC
Re: Stuck on something that should be so darn simple - link_to a "foo_bar" controller method?
There''s a punctuation error in your pasted example. I''ll let you find it. Next time, show us the actual code you are working with. On Sep 7, 6:46 pm, Rick R <ric...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Man, as a newbie as soon as I deviate just an inch from the hello > world stuff I''m in all sorts of hurt:)... > > I have a controller (lets call it GroceryStoreController) that has an > ''add'' method (It can be foobar for all I care at the moment, just > something that isn''t the conventional ones produced from scaffolding > and that you get mappings to automatically from ''resources'' definition > in Routes.) > > I now have a list of grocery store items and I want to provide an > ''add'' link next to each item. Hitting the add link should reach the > "add" method in the controller (passing along the id of the grocery > item.) > > This should be so simple, yet I can''t seem to get anything to work. I > don''t think I can just use my own xyz_path in link_to since I think > that''s just a convention for the built in resources (edit ,etc.) eg: > > link_to ''add'', add_grocery_store_path(item) #causes undefined path error > > I even tried doing things a way that docs say was and older way?: > > //sorry pasting from real code I was working with (results after you > do a search)... not grocery_store, but same idea... > > @meters.each do |m| > .... > link_to ''add'', controller => "meters_search", :action => "add", :id => m > > The above creates a link but it looks really bad: > > <a href="/meters_search/add?%23%3CMetersSearchController%3A0x00000102e90c48%3E =meters_search&id=1">add</a> > > and when you click on it it''s trying to find the ''show'' method (which > I don''t have in this controller, for now just have index, and add > until I get this working) so it breaks. > > Maybe it''s my Routes that is a problem, and I''ve been trying different > things there as well. Relevant routes for this example I have as: > > resources :meters_search > get "meters_search/add" => ''meters_search#add'' > > [ side not for any help in my learning: > I''m also am trying to following the docs for link_tohttp://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#... > and it''s extremely confusing. It shows some options but then also > mentions see options available by looking at "url_for" so when I click > on that linkhttp://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#... > I see seven options listed on the page ( anchor, only_path, etc.) yet > they keep showing examples of options like :action, :controller yet I > don''t see them shown anywhere in the API as valid options? ]-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.