This should be an easy question, but I just can't figure it out. I want to trigger an action (which just renders a page) through a button using "button_to" in view:
<%= button_to "Fresh", action: 'fresh', method: 'get' %>
The error says "No route matches [POST] "/static_pages/fresh"". It seems that the button still uses "post" instead the "get". Meanwhile, if I use "link_to", it works fine.
<%= link_to "Fresh", action: 'fresh', method: 'get' %>
Thanks for any comments and help.
Try this
<%= button_to "delete", {:controller => :static_pages, :action => 'fresh'}, :method => :get %>
Also check your routes.rb to ensure that the route to fresh is defined.
Related
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
I have this route:
delete 'basket/remove' => 'flowercard_baskorder#remove', as: :basket_remove
This link in view:
<%= link_to "Remove", basket_remove_path %>
And this in my controller (the binding is just for me to test):
def remove
binding.pry
end
When the link is clicked on nothing happens and i have no idea why!? I'm obviosuly expecting the binding to kick in but doesn't even look like a request is made?
Cheers
link_to defaults to a get request but you want it to go to a delete right? Just specify that in your call:
<%= link_to "Remove", basket_remove_path, method: :delete %>
You need to pass the method to the link_to function
<%= link_to "Remove", basket_remove_path, method: "delete" %>
I had my link for editing brands like this:
<%= link_to 'Edit', :action => 'edit', :id => brand %>
I found around here a way to make it look like a button, so I did this (not sure why the url helper method needs to be used, but with :action => 'edit' it didn't work, it kept thinking it was post action):
<%= button_to 'Edit', admin_brand_edit_path(brand), :method => "get" %>
However, the generated url is like this:
http://localhost:3000/admin/brand/edit.1
Where it should be
http://localhost:3000/admin/brand/edit/1
Result of rake routes for brand edit:
admin_brand_edit GET /admin/brand/edit(.:format) admin/brand#edit
admin_brand_update POST /admin/brand/update(.:format) admin/brand#update
routes.rb
get 'admin/brand/edit'
I am trying to generate a link that will use post method
<%= link_to "guess", :action => "submit_guess", :method => "post" %>
But when I click on that link I am getting
No route matches [GET] "/guess/submit_guess"
<%= button_to 'guess', action: 'submit_guess' %>
Or, if you have JS enabled:
<%= link_to 'guess', action: 'submit_guess', method: :post %>
# note the symbol
(It's better to use button_to and then style it with css if necessary)
<%= link_to "guess", {:action => "submit_guess"}, :method => "post" %>
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