Hi, I am trying to call a controller method from my view, but I get an error and I don''t know how to correct it. What i am trying to do: I am in the edit form of the model Product, and products can have many ingredients. So on my edit form there is a text_field where you type the ingredient with a link "Add" which is supposed to add the typed ingredient to the current ingredients list. this is how i try to call my controller method: <%=link_to "Add",:action => :add_ingredient,:id => @product %> This is my method in the controller: def add_ingredient if @product.id.blank? @product = Product.find(params[:id]) end @product.ingredients << Ingredient.find_or_create_by_name(params[:search]) @product.update_attributes(params[:product]) end The error: Unknown action No action responded to 30. Actions: add_ingredient, create, destroy, edit, index, new, show, and update the url: http://localhost:3000/products/30/add_ingredient Thanks Greg -- Posted via http://www.ruby-forum.com/. -- 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.
On Mar 10, 7:26 pm, Greg Ma <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> > this is how i try to call my controller method: > <%=link_to "Add",:action => :add_ingredient,:id => @product %> >Try this <%=link_to "Add",:action => "add_ingredient",:id => @product %> Note that the action name is a string. Hope this helps -- 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.
On Mar 11, 12:26 am, Greg Ma <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi, > I am trying to call a controller method from my view, but I get an error > and I don''t know how to correct it. > What i am trying to do: > I am in the edit form of the model Product, and products can have many > ingredients. So on my edit form there is a text_field where you type the > ingredient with a link "Add" which is supposed to add the typed > ingredient to the current ingredients list. > > this is how i try to call my controller method: > <%=link_to "Add",:action => :add_ingredient,:id => @product %> > > This is my method in the controller: > def add_ingredient > if @product.id.blank? > @product = Product.find(params[:id]) > end > @product.ingredients << > Ingredient.find_or_create_by_name(params[:search]) > @product.update_attributes(params[:product]) > end > > The error: > Unknown action > No action responded to 30. Actions: add_ingredient, create, destroy, > edit, index, new, show, and update > > the url:http://localhost:3000/products/30/add_ingredient >assuming that you''re using map.resources then you need to add add_ingredient as a member action (or you might decide that it is preferable to have ingredients as a nested resource of products) Either way you should really have a GET request that changes the state of the database Fred> Thanks > Greg > -- > Posted viahttp://www.ruby-forum.com/.-- 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.
Hi, Thanks it works better now. But how am i supposed to access my textfield if I am using a get method in my controller? Route: map.resources :products, :member => {:add_ingredient => :post } Controller: <%= text_field_with_auto_complete :product, :ingredient_name,{}, {:url => formatted_ingredients_path(:js), :method => :get, :with => "''search='' + element.value"} %> <%=link_to "Add",:action => "add_ingredient",:id => @product%> Frederick Cheung wrote:> On Mar 11, 12:26�am, Greg Ma <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> <%=link_to "Add",:action => :add_ingredient,:id => @product %> >> >> The error: >> Unknown action >> No action responded to 30. Actions: add_ingredient, create, destroy, >> edit, index, new, show, and update >> >> the url:http://localhost:3000/products/30/add_ingredient >> > > assuming that you''re using map.resources then you need to add > add_ingredient as a member action (or you might decide that it is > preferable to have ingredients as a nested resource of products) > Either way you should really have a GET request that changes the state > of the database > > Fred-- Posted via http://www.ruby-forum.com/. -- 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.
Greg, Since its a post request, you should have a form in your view, which when submitted, will ensure that all the text fields etc. are available in your controller. Punit Greg Ma wrote:> Hi, > Thanks it works better now. But how am i supposed to access my textfield > if I am using a get method in my controller? > > Route: > map.resources :products, > :member => {:add_ingredient => :post } > > Controller: > <%= text_field_with_auto_complete :product, :ingredient_name,{}, > {:url => formatted_ingredients_path(:js), :method => :get, :with => > "''search='' + element.value"} %> > <%=link_to "Add",:action => "add_ingredient",:id => @product%> > > > Frederick Cheung wrote: >> On Mar 11, 12:26�am, Greg Ma <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >>> <%=link_to "Add",:action => :add_ingredient,:id => @product %> >>> >>> The error: >>> Unknown action >>> No action responded to 30. Actions: add_ingredient, create, destroy, >>> edit, index, new, show, and update >>> >>> the url:http://localhost:3000/products/30/add_ingredient >>> >> >> assuming that you''re using map.resources then you need to add >> add_ingredient as a member action (or you might decide that it is >> preferable to have ingredients as a nested resource of products) >> Either way you should really have a GET request that changes the state >> of the database >> >> Fred-- Posted via http://www.ruby-forum.com/. -- 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.