:method put: not working instead Get method - ruby-on-rails

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.

Related

Rails button_to with image and actions

I want to show button with image.
I have this code
<%= image_submit_tag "down.png", controller: "posts", action: "votedown", post_id: post.id, topic_id: post.topic_id, class: "xta" %>
Its visible properly but not calling action "votedown"
In my routes I have
post '/votedown', to: 'posts#votedown
Please also suggest if there is any other way to call the method votedown with params and image "down.png"
image_submit_tag must be used in conjunction with a form - it works just a normal html <input type="submit"> button.
You might also want to change your route definition into something more restful:
patch '/posts/:id/votedown' => "posts#votedown", as: 'votedown_post'
This makes it more apparent that this route acts on a post - and we use the PATCH method since we are changing a resource instead of creating a new resource.
Armed with our new route we can simply create a form:
<%= form_for(#post, url: votedown_post_path(#post) ) do |f| %>
<%= image_submit_tag "down.png", class: "xta" %>
<% end %>
Note that you do not need to add an input for the post id since it will be available as params[:id].
Another way to do this would be to use Rails unobstructive javascript driver to create a link or button which sends a PATCH request to '/posts/:id/votedown'.
<%= link_to image_tag("down.png", class: "xta"), votedown_post_path(#post), method: :patch %>

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

Passing an extra param when submitting form generated with form_tag

I have a form:
<%= form_tag(example_path, method: :get) do %>
…
<% end %>
I would like like to pass an extra parameter along with the form params:
example: 1
I've tried passing a hash into the path helper, but the params are ignored:
form_tag(example_path(example: 1), method: :get)
How can I add this param without using a hidden field.
In order to what you want to accomplish, you need to update your route:
# config/routes.rb
get "example/:your_param" => "your_controller#example", :as => :example
And then, you will be able to do this in your view:
<%= form_tag (example_path('value'), method: :get) do %>
....
I hope this makes sense and help you.

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