rails 3 link_to put method not working - ruby-on-rails

I am using link_to helper and trying to set the method as :put as show in the documentation. Link
however when i see the method is falling back to get, what am i doing wrong. Here is my syntax
<%= link_to "Original One","/users/#{user.id}?params=#{session[:user]}", :remote => true,:method => :put %>

If you are using Rails 2.3, link_to won't work the way you expect it to work like in Rails 4. For Rails 2.3, you can use button_to, and pass the same options in it. Here's more you can find about it.
<%= button_to "Update", "/users/#{#user.id}", :method => :put, :remote => true %>

Related

Rails no action "edit" in forum_posts_controller

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

What is the correct button_to syntax for adding a class to the generated form?

I'm trying to apply a class to the form generated by button_to in Rails 3.
The :class option sets the class for the submit button so the docs tell us to use :form_class to apply a class to the form.
E.g.
<%= button_to 'x', user_contact_path(#user, contact), :method => :delete, :form_class => "delete" %>
This just adds the attribute form_class="delete" to the button element. I've tried various combinations using :html_options and so on.
Anybody know how to do this?
That method works perfectly fine for me. I am able to do:
<%= button_to "Hello", root_url, :method => :get, :form_class => "my_class" %>
the above generates the following:
<form action="http://localhost:3000/" class="my_class" method="get">
<div><input type="submit" value="Hello"></div>
</form>
However, this is in Rails 3.1 as the link in your question points and the same wouldn't work in Rails 3.0.x since the form class is hard coded.
From url_helper code:
("<form method=\"#{form_method}\" action=\"#{html_escape(url)}\"
#{"data-remote=\"true\"" if remote} class=\"button_to\"><div>" +
method_tag + tag("input", html_options) + request_token_tag +
"</div></form>"
).html_safe
Try with
<%= button_to 'x', user_contact_path(#user, contact), {:method => :delete, :form_class => "delete"} %>
This forces :form_class => "delete" to be part of the options hash instead of the html_options hash.

Convert snippet to Rails 3

I found a wonderful article
But it uses rails version previous to rails 3.
In particular, this snippet:
<%= link_to_remote( "click here",
:update => "time_div",
:url => { :action => :say_when },
:position => "after" ) %>
I converted it to this:
<%=button_to 'Click',:remote=>true,:update=>"time_div",:position=>"after",:action=>"say_when"%>
But, there's something wrong.The entire page is being rendered afresh.
What should be done to make it work as described on that site?
it is not button_to, it is link_to 'Click', :remote => true. Also you have to add csrf_meta_tag in the head tag in the layout for Rails 3. See link
In your layout <%= csrf_meta_tag %> And
link_to "some action", my_action_path(#post), :remote => true
For details Try this link
http://www.themodestrubyist.com/2010/02/24/rails-3-ujs-and-csrf-meta-tags/

Rails, how do you submit a form with a text link?

I am trying to get this form to submit correctly. Here's what I have so far:
<% form_for(:user, :url => update_user_setting_path, :remote => true, :html => {:method => :post, :class => "search_form general_form"}) do |f| %>
and the button renders with this code:
<li><%= link_to raw("<span class='button approve'><span><span>SAVE</span></span></span>"), :action => 'create' %></li>
I am using action create, is this correct?
Here is the rendered form tag:
<form method="post" data-remote="true" class="search_form general_form" action="/settings/2/update_user" accept-charset="UTF-8">
What am I missing? Thanks for your help!
No, you are not using link_to properly. You need to use a submit tag to submit your form, not a link_to tag, for example:
<% form_for(:user, :url => update_user_setting_path, :remote => true, :html => {:method => :post, :class => "search_form general_form"}) do |f| %>
...
<li><%= f.submit "Save" %></li>
If you want to use a text link you'll have to have javascript submit the form. For example, if you are using jQuery you could do the following:
<%= link_to 'Save', "#", :onclick=>"$('.search_form').submit()" %>
I like Pan's solution but I prefer to use the ID of the form directly which you can get from the dom_id(obj). The form_for helper also uses dom_id(obj) to assign the form's ID. This way you aren't dependent on setting classes by hand or subject to accidentally submitting more than one form that share the same CSS class. It looks a little stranger but I usually have a custom FormBuilder anyway so I just add a generic link_to_submit method to encapsulate this:
<%= link_to 'Save', "#", :onclick => "$('##{dom_id(#user)}').submit()" %>
You don't need to use an id or a selector if you have jquery, you can simply do :
= link_to 'Save', "#", onclick: "$(this).closest('form').submit()"
Thanks for the answers... I ended up using this and it works great:
<li><%= link_to raw("<span class='button approve'><span><span>SAVE</span></span></span>"), "index_users", :onclick=>"document.forms['form1'].submit();"%></li>

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