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.
Related
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 %>
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 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}%>
I am trying to create a button with an image. So basically, I need the button_to version of the code below :|
<%= link_to image_tag(product.image_url, :class => "img"), line_items_path(:product_id => product) %>
This is a pretty old post, but for future reference: since Rails 3.2.1 you can use button_tag instead of button_to, as the first one natively allows images:
Creates a button element that defines a submit button, reset button or a generic button which can be used in JavaScript, for example. You can use the button tag as a regular submit tag but it isn’t supported in legacy browsers. However, the button tag allows richer labels such as images and emphasis, so this helper will also accept a block.
As for your example:
<%= button_tag image_tag(product.image_url), line_items_path(:product_id => product), class: 'img' %>
I didn't test the code, but it should work. It is possible you need to declare the url with url:
This is my solution:
Use a button helper (you can use the button_to helper method):
<%= f.submit 'Save', :class => "button_with_image_save" %>
CSS:
.button_with_image_save {
background: url(../images/icons/page_save.png) #f2efa8 no-repeat 10px 6px;
text-indent:30px;
display:block;
cursor: pointer;
}
You can create a helper as button_to link -
<%= button_to product.image_url, line_items_path(:product_id => product) %>
and in application_helper
def button_to(image_path, link)
link_to (image_tag(image_path, :class => "img"), link)
end
I guess this is what you want.
The short answer is that you need to create a helper method, which is quite simple to do:
Here is a similar SO posting the explains it: Is there a way with rails form helper to produce a button tag for submit
Good luck
Image submit button:
<%= image_submit_tag("team/team1.png", class: 'image-responsive') %>
Link with image:
<%= link_to(image_tag("team/team1.png", class: 'image-responsive'), root_path, :method => :get) %>
Add the image is the folder app/assets/image
In the view
<%= image_submit_tag('nameimage.png') %>
the disadvantage is that you can not change the size with size, but you must have the image of the size you want to appear
I have been using submit tag helpers.
<%= submit_tag 'Submit' %>
I have an image called my_image.png.
How can I make the submit button be an image?
I tried:
<%= submit_tag '#{image_tag("Login_small.png", :width=>70)}' %>
But that doesn't work..
Check out image_submit_tag, it is what you are looking for. It is used like image_submit_tag("login.png")
I believe you'd like to use image_submit_tag.
See the docs for more details.