Getting button_to to act like link_to - Rails - ruby-on-rails

I have a button_to call that I essential want to act like a link_to call. I set it up to use a get request but when I click it, in the URL a '?' appears on the end of the URL.
Ex: /admins/new? instead of /admins/new. How do I remove this ? from the URL so it behaves just like a link_to link?
Button_to code
<%= button_to "New Admin", new_admin_path, :method => :get %>

Take a look at your own question:
The red arrow points to elements that look like a button but in fact are links, just styled. You can do the same in your app.

You can customize the method in button_to like such:
<%=button_to "Admin",new_admin_path,{method: :get}%>

Related

how to put controller/action in link_to rails 4.1

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

Ruby on rails submit button

am new in ruby on rails and please i want to execute a method in my controller or in my helper when i click this button, so any ideas for that?
<%= button_to 'Export POINT', :action => :create_file_txt %>
Thanks
Route
button_to is basically a link:
Generates a form containing a single button that submits to the URL
created by the set of options. This is the safest method to ensure
links that cause changes to your data are not triggered by search bots
or accelerators. If the HTML button does not work with your layout,
you can also consider using the link_to method with the :method
modifier as described in the link_to documentation
You need to send it to a route:
#config/routes.rb
get "your_route", to: "controller#action"
This will give you the ability to use the URL helpers to define the path & get the button to work:
<%= button_to 'Export POINT', your_route_path %>
Link
As per the comments, you would also benefit from using link_to for this as well:
<%= link_to 'Export POINT', your_route_path %>

Generate link_to without path (only anchor)

I need to have path like "#privacy" for tab plugin. Link must contain only anchor. When I use link_to 'Privacy', :anchor => 'privacy' Rails generate /privacy#privacy - link, that contains full path and anchor.
How can I told Rails to generate url without path (only anchor)?
Thanks.
Solved: link_to 'Privacy', '#privacy'
The following will create a link the way you want -
link_to "my-privacy", "#privacy"
In most browsers, the path of the current page will be prefixed, but if you check the source of the page, the following html will be seen -
my-privacy
This will most probably serve your purpose for the UI, just that you'll have to split the url at '#' using Javascript.
This will work for you
<%= link_to "title", resource_path(:anchor => "anchor") %>
No generation of routes is needed also no generator is needed.
<%= link_to "link text", "#", :id => 'your_id_here' %>
You will need the id to access the object via jQuery.
//edit
<%= link_to "link text", "/#anchor", :id => 'your_id_here' %>
Your best bet for this exact scenario is to just use <%= link_to "link text", "#anchor" %>. The anchor tag is used within url_for and doesn't really give you a clean way to just use an anchor.

rails create a button for downloading

I need to create a button for downloading
So far I have create a button using this
<%= form_tag '/myproject/download',
:multipart => true,
:onclick=>"document.getElementById('popup').style.display='block';
return true;" %>
<%= submit_tag 'Download'%>
<% end %>
but I don"t know how to link the submit_tag with the link of downloading. (so as simple as if I press the button, then it will go to localhost/file and pop up a save to option)
I am aware that I can use link_to, but I need to put a button that acts as a link_to
Can anyone point out how to do this in rails? thank you
Try button_to method, which is nearly identical to link_to
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to
You can put a link_to and then style it like a button. Or you can do something like this Is there a way with rails form helper to produce a button tag for submit
Try to use the button_to method. You can take a look to the docs.

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