Form with method: "get" makes a POST request? - ruby-on-rails

This is my form:
<%= form_tag(method: "get") do %>
<%= submit_tag("Submit") %>
<% end %>
When I submit this form I get a server error because there is no POST action for this URL. In my routes I have an action for GET, but it's not picked up. The error goes away when I assign an action to POST at the same URL as the GET. What am I doing wrong?

Like the comment above says, you'll need to add a path to the form, so it would look something like this...
<%= form_tag whatever_the_current_page_is_path, :method => :get %>

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

Cannot get delete request to work - rails

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

How would I condense this rails form into one button?

Here's my form:
<%= form_for #asset do |f| %>
<%= f.check_box :remove_picture %>
<%= f.submit "Remove" %>
<% end %>
How could I just make this one button that does :remove_picture and submit? Thanks
Check out the API dock for the form_for helper:
http://apidock.com/rails/ActionView/Helpers/FormHelper/form_for
You can force the form to use the full array of HTTP verbs by setting
:method => (:get|:post|:put|:delete)
So your code might look like
<%= form_for(#asset, html: { method: :delete }) do |f| %>
<%= f.submit "Remove" %>
<% end %>
You could change the checkbox to a hidden field on the form...
If it were me, I'd look at something like button_to and handle this via AJAX on the controller. This way the button would call a controller action, say remove_picture and return a JS response which could update your view.
Example:
button_to([remove_picture, #asset], { method: :delete })
Note: method: :delete may not be needed - depends on your routes.

rails 4 update user profile by button

hy, i'm trying to update a single field in a user resouce. The field to update is 'locale', it is a string type.
i have triend with a form and it works:
<%= form_for current_user do |f| %>
<%= f.select :locale, [['En', 'en'], ['It', 'it']] %>
<%= f.submit %>
<% end %>
now i'm trying to update the same field using a link or a button:
<%= current_user.locale = 'fr' %>
<%= button_to "update" ,current_user,method: :patch %>
<%= button_to "update" ,user_path(current_user),method: :patch %>
but none of this work.
the problem is the request, infact the web server doesn't recive the current_user parameters:
{"_method"=>"patch",
"authenticity_token"=>"7X6QdD4DGxsXaETT86/8Ut4xyuOICaxirs4IjmZl7jY=",
"locale"=>"en",
"id"=>"1"}
There is no current_users parameters.
i have tried with the link_to but the problem is the same:
<%= link_to "update", current_user ,method: :patch %>
I have no idea. Can you help me?
The fact that you don't get the parameter sent back to server is the expected one since you don't use a form.
In order to pass the parameter back you have to:
use a form (as you did) or
pass the parameter in the url of the button: button_to 'update', user_path(param_name: 'param_value')
In the second case, you will have to search for the appropriate parameter in your action, ex params[:param_name]

How do you post a value from a form_tag?

If you have a post action to "do_something/:id" => :start (say with a named route 'start'), how do you create a form_tag that submits the :id based on a select_tag selection by the user ?
Assuming you aren't using resources.
The issue is that you have a post action, however you want to set a GET variable via a form submission(that has to use POST).
You can make the form make a GET request, like so:
<%= form_tag(start_path, :method => "get") do %>
<%= select_tag "id", "<option>1</option><option>2</option><option>3</option><option>4</option>" %>
<% end %>

Resources