Jack
Jack

Reputation: 19

how to put controller/action in link_to rails 4.1

I have:

<%= button_to '+',{:controller=>"line_items",:action=>'create',:menu_id=> line_item.menu_item,:remote=>true}%>

I have put same code for link:

<%= link_to '+',{:controller=>"line_items",:action=>'create',:menu_id=> line_item.menu_item,:remote=>true}%>

But link_to is redirect me to the line_item_index page.I want that link_to will work like this button.please help me i am new in rails.

Upvotes: 0

Views: 222

Answers (2)

sa77
sa77

Reputation: 3603

link_to uses http GET for the resource you're linking while button_to uses http POST to your controller action. adding :method => :post explicitly to your link_to tag makes it behave as http POST event

Upvotes: 2

Marko Krstic
Marko Krstic

Reputation: 1447

method link_to creates link and method button_to creates form. They are not the same. From button_to you are using the form with method POST and it works ok, but with link_to you are using method GET, and this is a problem.

To solve the problem, try this:

link_to '+',{:controller=>"line_items",:action=>'create', :menu_id=> line_item.menu_item, :remote=>true, method: :post}

more explanation at:

http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to

Upvotes: 0

Related Questions