Ruby on rails submit button - ruby-on-rails

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

Related

Getting button_to to act like link_to - 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}%>

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

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