HAML - how do I make a path that makes the browser go back in a button? - haml

I currently have a button like this:
= button_to "Go Back" , mobile_disclosures_url , :method => :get
But I am not sure what path to give it so that it makes the browser go back. Any help? :)
Thanks!

You have to use JS for a real browser based backward navigation.
= button_to "Go Back", '#', :onclick => 'history.back(); return false;'
From memory, I think works with button_to, but it should definitely work with link_to.

=link_to "Go Back", :back
and it will set the link to the REFERER header

Related

User click on image to download public file in rails

I'd like a user of my rails app to be able to click on a link 'download' and that they will then download a png file I have placed in my public folder. ('tool.png')
At the moment I have the incorrect...
<%= link_to "download", '/tool.png', :action => 'download' %>
I have created a download action in the controller:
def download
send_file '/tool.png', :type=>"application/png", :x_sendfile=>true
end
What is happening here is that when a user clicks on the 'download' link it opens tool.png in its own window, rather than actually downloading the file.
Can anyone help me with this?
Thanks
HTML 5
For HTML5 it's actually very simple. You don't need a special controller action.
<%= link_to "download", '/tool.png', :download => 'filename' %>
# 'filename' specifies the new name for the downloaded file
Note: check the docs to see what browsers are supported
< HTML 5
If you want to support all browsers you must use the download action which you setup. The only thing missing is setting up the correct route.
routes.rb
get '/download_pdf', "controller_name#download", :as => :download_pdf
then link your HTML to the correct path, which will call the download action and send the file.
<%= link_to "download", :download_pdf_path
What you need is
<%= link_to "download", '/download', :action => 'download' %>
not
<%= link_to "download", '/tool.png', :action => 'download' %>
Where "/download" is the rails route which need to be specified in routing file.
since in your case your are not actually hitting the controller, you are just accessing http://host/tool.png. Check your development logs for more info, you will see no logs since request is not directly served by rails but with other case you will see them.

How to use :onclick and :confirm together in link_to Rails 3?

I am trying to hide a div on click, after confirmation from the user.
I have something like this:
<%= link_to 'X', user, :method => :delete, :remote => true, :class => 'delete_link', :onclick => "jQuery('#user_#{user.id}').hide();" %>
Whenever the 'x' is clicked, I get a confirmation popup, but onclick event is not executed.
I want to hide the div whenever user gives Yes in the popup. How do I do this in rails 3 way?
You can hide immediately after click this way.
$('.delete-item[data-remote]')
.data('type', 'html')
.on('ajax:before', function(event, data) {
$(this).closest('.item').remove();
});
If you want to wait until the response is back, replace 'ajax:before' with 'ajax:success'
You can use the other way, rather rails 3 way. You can hide your div by having destroy.js.erb. and write you div hidding Jquery code there, #user_#{user.id}').hide();
Hope, this will help you :)

Rails 3 Apply CSS class if path matches root:to path

I am very new to ROR and I love it so far as I develop my first app. I have a question related to my application template as I apply formatting to the nav menu.
Is it possible to check if a url path matches the root:to path set in config.rb? I have a helper method that returns the string "current" which adds the css class style to highlight the selected menu item. The helper method works fine as long as I'm not at the homepage. When I my url is www.localhost:3000/ the css current class is not applied to the Products link since the request_uri = "/" which doesn't equal "/products". I would like the css class "current" applied to the Products menu item when I'm on the homepage.
Is there any conditional logic I can use to get the root:to path and check if it matches the is_current's parameter path?
Here's my code:
routes.rb root:to setto point to the products index view
root :to => 'products#index'
application.html.erb
<%= link_to 'Products', products_path, :class => is_current(products_path) %>
<%= link_to 'Reports', reports_path , :class => is_current(reports_path) %>
application_helper.rb
def is_current(path)
if request.request_uri == path
return 'current'
end
end
Any help would be greatly appreciated.
Thanks,
bkasen
Would this work for you?
if current_page? root_path
for more info: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-current_page%3F
If I read correctly, you want "onlink" css applied? so that when the user is on the home page, the home icon is a different color or something. If so then apply this in your header partial or where ever:
<li><%= link_to_unless_current "Home", root_path, :class => "navLinks" do link_to "Home", root_path, :class => "navLinks", :id => "onlink" end %></li>
I know this is an old question, but it may prove useful to someone else :)

Simulate PUT on a link, in Rails3

So far, I know that in Rails I can simulate a PUT request using a form which has a hidden input with name='_method' and value='put', but I'm interested in simulating that for a link.
How can I have a link in a view that would fit this route:
match '/article/:id/publish', :to => 'article#publish', :via => :put
The docs for link_to say you can specify a :method option that creates a form that is submitted on clicking the link.
link_to "Publish!", publish_article_path(article), :method => :put
Not sure what your route helper method would be (I assumed publish_article_path - you should be able to figure it out with rake routes from the command line. The :method is the important part that will do the magic you want.

help with a rails ajax button_to :remote

Hi Im trying to make a button_to :remote with Rails3, which when onclick it will fetech some content and display success alert when ajax did a round trip back with status 200.
my code now =
<%= button_to 'helloworld', '/ccs', :remote => true ,:method=>'get', :disable_with => 'loading', :confirm => "are u sure?", :success =>"alert('lol');" , :failure=>"alert('omg');"%>
It dose send another HTTP request when button clicked but just not having any action on success or failure.
What's wrong with it anyone?
Rails 3 no longer has support for prototype helpers and their callbacks, such as :success and :failure.
Read more on this page, particularly sections 3 and 4:
http://www.simonecarletti.com/blog/2010/06/unobtrusive-javascript-in-rails-3/
As you can see, you'll have to bind those callbacks manually (the page above uses jQuery), but you won't be able to do so inline.
Alternatively, button_to_remote, which will do exactly what you want, is now available as a plugin:
http://github.com/rails/prototype_legacy_helper