# Call a controller method from a view

**URL:** <https://discuss.rubyonrails.org/t/call-a-controller-method-from-a-view/50054>\
**Category:** rubyonrails-talk\
**Created:** [March 11, 2010, 12:26am UTC](https://discuss.rubyonrails.org/t/call-a-controller-method-from-a-view/50054 "2010-03-11T00:26:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [March 11, 2010, 12:26am UTC](https://discuss.rubyonrails.org/t/call-a-controller-method-from-a-view/50054/1 "2010-03-11T00:26:52Z")

</div>

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: &nbsp;&nbsp;def add\_ingredient &nbsp;&nbsp;&nbsp;&nbsp;if @product.id.blank? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@product = Product.find(params[:id]) &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;@product.ingredients \<\< Ingredient.find\_or\_create\_by\_name(params[:search]) &nbsp;&nbsp;&nbsp;&nbsp;@product.update\_attributes(params[:product]) &nbsp;&nbsp;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](http://localhost:3000/products/30/add_ingredient)

Thanks Greg

---

<div class="post-metadata">

**Author:** ![Hiro](https://avatars.discourse-cdn.com/v4/letter/h/b5ac83/32.png) [@Hiro](https://discuss.rubyonrails.org/u/Hiro)\
**Post date:** [March 11, 2010, 5:47am UTC](https://discuss.rubyonrails.org/t/call-a-controller-method-from-a-view/50054/2 "2010-03-11T05:47:27Z")

</div>

Try this \<%=link\_to "Add",:action =\> "add\_ingredient",:id =\> @product %\>

Note that the action name is a string.

Hope this helps

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [March 11, 2010, 8:13am UTC](https://discuss.rubyonrails.org/t/call-a-controller-method-from-a-view/50054/3 "2010-03-11T08:13:01Z")

</div>

> 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](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

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [March 11, 2010, 3:35pm UTC](https://discuss.rubyonrails.org/t/call-a-controller-method-from-a-view/50054/4 "2010-03-11T15:35:06Z")

</div>

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: &nbsp;&nbsp;map.resources :products, &nbsp;&nbsp;&nbsp;&nbsp;:member =\> {:add\_ingredient =\> :post }

Controller: \<%= text\_field\_with\_auto\_complete :product, :ingredient\_name,{}, &nbsp;&nbsp;&nbsp;&nbsp;{:url =\> formatted\_ingredients\_path(:js), :method =\> :get, :with =\> "'search=' + element.value"} %\> \<%=link\_to "Add",:action =\> "add\_ingredient",:id =\> @product%\>

Frederick Cheung wrote:

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [March 11, 2010, 9:35pm UTC](https://discuss.rubyonrails.org/t/call-a-controller-method-from-a-view/50054/5 "2010-03-11T21:35:35Z")

</div>

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:
