Button_to in Ruby on Rails bad route - ruby-on-rails

I'm trying to use the button_to rails helper. I wrote the following code:
<%= button_to 'Edit Item', edit_item_path(#item), :class => 'mark-button' %>
and got the following error message
No route matches "/items/1/edit"
But when I refresh the page it goes to the appropriate action. The URL of the page i get is localhost:3000/items/1/edit which is the correct URL. If I switch the button_to command to link_to the page loaded with no errors. Meaning this code:
<%= link_to 'Edit Item', edit_item_path(#item), :class => 'mark-button' %>
loads fine. Maybe there is some feature of button_to I'm not aware of, but I am at a lost.

I think you might be misusing button_to. I've always thought that if you're linking to the edit action, you should be using link_to. Buttons seem to be for actions that need to post/put data such as updating a form or deleting a record.
Update:
By default, button_to uses POST instead of GET. Hence it working when you just visit the URL (ie GET).

button_to defaults to POST, and link_to defaults to GET.
If you really need button_to you can change the default method to GET for edit and other links.
for ex:
<%= button_to 'Edit', edit_user_path(#user), :method => :get %>

Ruby -v 2.8.6, Rails 6.1.4.1
<%= button_to 'Edit', edit_item_path(item), :method => :get %> because with expression (#item) you do not define the object you want to edit, because (#item) it is not a specific object, there are several and you need to define only the one you want to edit, :method => :get this method is perfect

Related

Methods with link_to In ruby tags (ruby on rails)

I am working on a Ruby on Rails project, and I noticed that the link_to can either work with or without a method specified.
With the method specified:
<%= link_to "Log In", new_user_session_path, class: 'btn btn-primary navbar btn', method: :get %>
Without the method specified:
<%= link_to "About", about_path, class: 'navbar-brand' %>
How do I know when should I use a method with link_to, and when I should not?
From my understanding, you should use method when it's different from get. Eg, I specify it when I need to use post and so on.
It tells the browser which HTTP method to use when submitting the request to the web server. Supported verbs are :post, :get, :delete, :patch, and :put.
You can always specify it if you want to be explicit, otherwise it's only necessary when you need it to deviate from the default. Different functions have different defaults.
button_to defaults to :post
link_to defaults to :get
etc.
This link has many examples and a more thorough explanation:
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html

Rails no action "edit" in forum_posts_controller

The forum_posts controller is located in app/controllers/forum_threads/forum_posts_controller.rb
I don't know if I have to call forum_threads:forum_posts in the link_to.
Controller:
http://pastebin.com/t9vuyxdP
HTML:
http://pastebin.com/LextuZ74
On a side note, how do you add a button to the link_to? I've tried adding :class => "button" at the end, doesn't cause an error but still just shows a link not a button.
If you want to use link_to with older-style controller/action/id arguments:
<%= link_to "Edit", :controller => "forum_threads/forum_posts", :action => "edit", :id => forum_post.id, :forum_thread_id => #forum_thread.id %>
But Rails prefer newer RESTful routes whenever possible. If you define your controller correctly in routes.rb, you can write link_to like this:
<%= link_to "Edit", edit_forum_thread_forum_post_path(#forum_thread, forum_post) %>
UPDATE 18/11/2015: You forgot to include forum_thread_id in link_to as I saw your route require forum_thread_id and id param to make it work.
For more information on link_to, refer to: http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to

rails 3 link_to put method not working

I am using link_to helper and trying to set the method as :put as show in the documentation. Link
however when i see the method is falling back to get, what am i doing wrong. Here is my syntax
<%= link_to "Original One","/users/#{user.id}?params=#{session[:user]}", :remote => true,:method => :put %>
If you are using Rails 2.3, link_to won't work the way you expect it to work like in Rails 4. For Rails 2.3, you can use button_to, and pass the same options in it. Here's more you can find about it.
<%= button_to "Update", "/users/#{#user.id}", :method => :put, :remote => true %>

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.
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
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

Rails button_to fails with path doesn't exist for a path that exists

Writing my first, very simple Rails application, a simple admin app to track work for one of our departments. The generated index page for people has a link_to on it to add a new person. I tried to change that to button_to and it fails saying the path /people/new doesn't exist, though obviously it does since link_to goes to the same place.
I'm using Rails 3/Ruby 1.9.2. I have this code on my /app/views/people/index.html.erb page:
<%= link_to 'New Person', new_person_path %>
<%= button_to "New", :controller => "people", :action => "new" %>
The link_to works. The button_to fails with this:
Routing Error
No route matches "/people/new"
Also tried just
<%= button_to 'New Person', new_person_path %>
Same error. Odd.
button_to defaults to the post method. Try putting :method => :get in there. This is why link_to works.
There's a good explanation for this, as always :)
link_to uses GET as default, where button_to uses POST. And there's no POST route that matches, only a GET route.
If you want to use button_to, you can add :method => :get to your buttons params and it will use GET.
Did you set up your routing options in config/routes.rb? Check if you have this in your routes.rb file:
resources :people
Check this guide for more informations about how routes work.
Is your button_to inside a form? button_to creates a form of its own so this would create a form within a form and likely break routing.

Resources