I am trying to implement a link in rails that will point to a certain url with a delete: method and with some params in the url, what I am trying is this:
<%= link_to "Remove", [:remove_country, :admin, #species, my_param: country.id ], method: :delete %>
Note the my_param: country.id I am trying to pass in. This gives me the result:
undefined method `model_name' for Hash:Class
due to the hash in the params. Unfortunately I need that there as I am trying to pass an extra param into the url (my_param which is country.id in this example)
as far as I can remember this is the correct method to pass params into a link_to in rails. The normal link_to that I use (without the my_param) is below, that works perfectly:
<%= link_to "Remove from this species", url_for([:remove_country, :admin, #species]), method: :delete %>
So how do I incorporate my param into this url? thanks in advance. (here is a source for why I think this should work)
Use polymorphic_path([:remove_country, :admin, #species], {country_id: country.id}).
Ok, I made a hacky solution but I don't like it one bit:
<%= link_to "Remove", url_for([:remove_country, :admin, #species]) + "?country_id=#{country.id}", method: :delete %>
It's very ugly, please someone put me out of my misery and show me a nice way to do this.
Related
I have article model and i want to give it votable ability with acts_as_votable gem.I have like_article route for like article and its PUT method and i have that route in my file
<%= link_to like_article_path(article), method: :put do %>
Like
<%= article.get_upvotes.size %>
<% end %>
if i hit the like button i'm getting No route matches [GET] "/articles/11/like" error it act like get method but i gave method: :put parameter it should be put why it dowsnt see my parameter how can i fix that?
I dont know why but i used button_to instead of link_to it worked.
I am building a small social web app and users should be able to like other user's posts. Here is my link_to for 'liking' a post.
<%= link_to "like", like_post_path(post), method: :post do %>
And here is the error message I get:
undefined method `stringify_keys' for "/posts/5/like":String
Originally this said posts/6/like, but I deleted that post in console and now it says 5.
I am new to rails and I have no idea what this means. If you need any more code, let me know. I am trying to figure out what the problem is.
When you use the block way for link_to, the first value is the path, and you put the value in the block, everything else comes after the path is considered as a hash, and rails calls stringify_keys on it. In your case, you put 'like' as the first argument, which rails considered as the path, and like_post_path(post) is simply a string without a pair. henceforth the error
Either
<%= link_to like_post_path(post), method: :post do %>
like
<% end %>
Or just use the one liner, without the do
<%= link_to "like", like_post_path(post), method: :post %>
So I am trying to use the following route:
<%= link_to like_post_path(#post), :method => :put do %>
But I don't know why is using GET method instead of PUT
No route matches [GET] "/posts/1/like"
makes no sense to me..
myroutes.rb:
resources :posts do
member do
put "like" => "posts#upvote"
put "dislike" => "posts#downvote"
end
You are using correct format and your code is should generate something like this:
<a data-method="put" href="...">
From your routes error message we can conclude that it is not being sent using POST with _method=put params. So, the problem must be that you did not include jQuery and Rails jQuery extension javascript files.
An easy fix would be to include application.js file (jquery and rails js extension are included by default) in your page.
link_to signature :
link_to(name = nil, options = nil, html_options = nil, &block)
According to the doc, :method belongs to the options hash.
calling
<%= link_to like_post_path(#post), :method => :put do %>
results in putting into
name : like_post_path(#post),
options: nil,
html_options: { method: :put }
From Hash
Hashes are also commonly used as a way to have named parameters in functions. Note that no brackets are used below. If a hash is the last argument on a method call, no braces are needed, thus creating a really clean interface:
EDIT
<%= link_to like_post_path(#post), { method: :put } do %>
leads to what you are trying to achieve.
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
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