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.
Related
I am stuck with a strange issue. I have googled and tried everything that was possible. All in vain and I am at the same place.
Listing the details
I have a route in routes file as resources :projects
The routes generated is as
admin_projects GET
/admin/projects(.:format) admin/projects#index
POST /admin/projects(.:format) admin/projects#create
new_admin_project GET /admin/projects/new(.:format) admin/projects#new
edit_admin_project GET /admin/projects/:id/edit(.:format) admin/projects#edit
admin_project GET /admin/projects/:id(.:format) admin/projects#show
PUT /admin/projects/:id(.:format) admin/projects#update
DELETE /admin/projects/:id(.:format) admin/projects#destroy*
My form is
form_for [:admin, #project], format: :js,remote: true, html: {id: 'edit-project-form', :method => :put } do |f|
...form fields
<td><%= f.submit 'Save', class: "btn primary save" %></td>
The URL being generated and the form tag on inspection is as follow
IF i manually update this post to put through firebug it gets updated Otherwise its throwing me a no routes matched error
(No route matches [POST] "/admin/projects/46.js"):
Please Help me figuring out whats going wrong.
remove 'format: :js' as 'remote: true' will send this as js request.
that is causing the whole issue.
I am trying to generate a url in an actionmailer template. An example if the url I want to generate is
http://0.0.0.0:3000/users/confirm/lNbQxzFukYtEEw2RMCA
Where the last segment is a hash to identify the user
However when I use this
<%= url_for(:controller => 'users', :action => 'confirm', :id => #user.confirmhash, :only_path => false) %>
It generates this
http://0.0.0.0:3000/assets?action=confirm&controller=users&id=ZOR3dNMls8533T8hJUfCJw
How can I get it to correctly format? I have no idea where 'assets' is coming from.
Is there an easier way to use named routes that I am missing?
I've found the answer. As I'm still learning I've missed the option to create a named route. So this this the path I've taken.
In config/routes.rb
match 'user/confirm/:id' => 'users#confirm', :as => :confirm_account
Then in my action mailer template I've used
<%= link_to "Confirm your account", confirm_account_url(#user.confirmhash) %>
Which passes the :id into the controller action.
I'm trying to implement an 'edit' link that brings up a form to change a displayed attribute on a page.
My layout has:
<div id="company_info">
<%= yield :company_info %>
</div>
<div id="edit_company_info">
</div>
My view has:
<%= content_for :company_info do %>
<%= render 'company_info' %>
<%= link_to "Edit", 'company_info_form', :class => 'btn btn-mini', :method => :get, :remote => true %>
My controller has:
def company_info_form
#company = Company.get(params[:id])
respond_to do |format|
format.js
end
end
My company_info_form.js.erb file has:
$('#edit_company_info').html("<%= escape_javascript(render "company_info_form") %>");
Upon clicking the link, my server shows:
Started GET "/companies/company_info_form" for 127.0.0.1 at 2012-03-12 20:19:13 -0700
Processing by CompaniesController#show as JS
Parameters: {"id"=>"company_info_form"}
Completed 500 Internal Server Error in 1ms
RuntimeError (Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id):
app/controllers/companies_controller.rb:9:in `show'
So I think this is a routing issue-- but I have no idea how to fix it. How do I get the company_id param that is on my current page to be recognized by the partial I'm loading as well?
I'm on /companies/1, but the link is to companies/company_info_form, losing the "company_id = 1" param.
Yes, the issue is with your routes and with your link as you have pointed out.
The first issue can be ascertained as it says Processing by CompaniesController#show as JS. So, its actually going to companies#show where it tries to find a company based on id. But, since no correct id is passed, it errors out.
The second issue is because your link is to companies/company_info_form, as you pointed out, since you have used 'company_info_form' as the path in your link for edit. And you haven't passed current company to the link either.
Since you haven't posted your routes file, which you should have, since you have identified a potential problem with routes , I'll present my own.
In your routes :
resources :companies do
member do
get 'company_info_form'
end
end
That will provide you with
company_info_form_company GET /companies/:id/company_info_form(.:format) companies#company_info_form
Then you can provide the link as :
<%= link_to "Edit", company_info_form_company_path(#company) %>
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 :)
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.