undefined method `stringify_keys' for "/posts/5/like":String - ruby-on-rails

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

Related

Http request work differently from the parameter I gave in Ruby on Rails

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.

What is causing this undefined method `model_name' for Hash:Class

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.

No Route Matches Rails 4

I generated a scaffold for Studio. Now am linking from Products view
<%= button_to 'Customize', new_studio_path %>
resources :studios is placed in routes.rb
I am still getting No route matches [POST] "/studios/new"
If someone could help me figure this out I would appreciate it tons.
Here is a gists of the files I am working with for this. https://gist.github.com/JRizzle88/7861628
Button's default HTTP method is POST, what you need is method GET, so you need to specify this explicitly:
<%= button_to 'Customize', new_studio_path, method: :get %>
Button is sending a POST request, where your server expects a GET request for given url. You need to specify method on a button:
<%= button_to 'Customize', new_studio_path, method: :get %>
The rails convention would be to use link_to which will send a GET request:
<%= link_to 'Customize', new_studio_path %>

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.

Button_to in Ruby on Rails bad route

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

Resources