As I understand it, Ajax allows you to only refresh the part of the page that you specify, as opposed to reloading the whole thing. Is there a way of doing this with a link_to method, for example, <%= link_to 'approve', approve_model_path(#model.id), method: :put %> ? I can't figure out how a link_to could be executed without the whole page being reloaded.
You can apply ajax by setting remote to true:
<%= link_to 'approve', approve_model_path(#model.id), method: :put, remote: true %>
This does a js request, then you need an approve.js.erb to do the few changes you need (without refreshing the page)
Related
In a Rails (5.2.4.2) app
I am using button_to + remote: true, and this is working as expected -> ajax call fired, all is OK.
But then I add 'params' to the button_to. Params are added to the form as hidden input (as expected) but when I click on the button, the request made is not remote, and all the page content is updated.
Problematic: button_to + remote + params -> this code seems to ignore the remote:true, although I see data-remote="true" in the form tag
<%= button_to ent_path(ent),
{remote: true,
method: :patch,
class:"btn btn-primary",
params: {ent: {active_a: false}}
} do
%>
<span>TEEEST</span>
<% end %>
Working as expected:
<%= button_to ent_path(ent),
{remote: true,
method: :patch,
class:"btn btn-primary",
} do
%>
<span>TEEEST</span>
<% end %>
So my purpose is to have the button_to remote and update the ent record, changing the active_a attribute depending on some logic
You want to pass any extra parameters via the link path, like this
<%= button_to ent_path(ent, active_a: false),
{remote: true,
method: :patch,
class:"btn btn-primary",
} do
%>
<span>TEEEST</span>
<% end %>
Turned out the remote:true was working properly. But there was a redirect_back in the controller that was executed in case of some custom logic, so that appeared as if the button is working without remote:true
I am working on a Ruby on Rails project, and I noticed that the link_to can either work with or without a method specified.
With the method specified:
<%= link_to "Log In", new_user_session_path, class: 'btn btn-primary navbar btn', method: :get %>
Without the method specified:
<%= link_to "About", about_path, class: 'navbar-brand' %>
How do I know when should I use a method with link_to, and when I should not?
From my understanding, you should use method when it's different from get. Eg, I specify it when I need to use post and so on.
It tells the browser which HTTP method to use when submitting the request to the web server. Supported verbs are :post, :get, :delete, :patch, and :put.
You can always specify it if you want to be explicit, otherwise it's only necessary when you need it to deviate from the default. Different functions have different defaults.
button_to defaults to :post
link_to defaults to :get
etc.
This link has many examples and a more thorough explanation:
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html
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 submit a form using link_to as follows:
<%= form_for(#post, :url=> '/post/action', :method=> 'post', :html => {:id=>'form_id'} ) do |f| %>
....
<%= link_to 'submit', "/post/action", :onclick=>"document.getElementById('form_id').submit()" %>
....
but it is not posting the form, it is simply redirecting my form to the specified url. Does anyone know how to do this?
You can use:
<%= link_to 'submit', "#", :onclick => "$('#form_id').submit()" %>
if you are using JQuery and a later version of rails.
Above will work but if you have a really long page it will navigate to top of the page because of "#" so if you want to avoid that you can do:
<%= link_to 'submit', "", :onclick => "$('#form_id').submit()" %>
I think both things happen. The browser starts to submit the form, but it also follows the link's href. You can fix it by linking to # instead of /post/action...
...however, I don't recommend doing it. There are a few better approaches:
First, you can use a button instead of a link. You'll have to style it to make it look like a link, but that should not be a problem. It will be better, because it won't break the Principle of Least Surprise (people who read the code expect forms to be submitted with buttons) and you won't need the JavaScript.
If you insist on using a link, you should at least move the JavaScript code from the view to a JavaScript file. Then have this behavior added unobtrusively (although, you won't have a good fallback with the link). Assuming you're using jQuery, it should be as simple as:
$(document).on('click', '[data-submit-form]', function(e) {
e.preventDefault();
$(this).closest('form').submit()
}
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