How to use send_file in RoR - ruby-on-rails-3

I'm very new to RoR and am trying to use send_file to have users download and view files that were uploaded by other users. I want to have a link on the project show page for each of the uploaded files. For now I am uploading the files using carrierwave and storing them in public/uploads/permit/avatar/permit_id/file. After going through much documentation I can't seem to figure out exactly what code to put into the model view and controller to get the links to show up on the show page. Can some one please help me with what code to put where.

if you want allow only authorithed users download files then you should set other path than public, something like:
private/uploads/permit/avatar/permit_id/file
and use something like send_file avatar.uploader_field.file in controller action then in views:
link_to "avatar", path_to_controller_action
For everyone:
link_to "avatar", avatar.uploader_field.url
or
link_to "avatar", avatar.uploader_field.path
in views

Related

Rails3. Is there any way to separate javascript file for different controllers?

I search around for quite some time but there's are no close solution for this.
For example,
I had generated 2 controllers: Articles, and Calendars.
There have been 2 javascript files also been generated according to these 2 controllers as:
calendars.js and articles.js.
The problem is, if they (2 files) all are included into application.js without any condition. i.e when I open articles/index action, the calendars.js has been imported on page
or when I open calendars/index action (or any action), the articles.js has been imported on page.
What I am looking for is a methodology to separate javascripts file according to the controller that they are belong to?
I also having the same question related to css files.
Thank you and Best regards,
Dat
What you probably want to do is only execute certain pieces of javascript on each controller (action). You can achieve that easily with this gem: https://github.com/intrica/rails_document_ready.
You can add in your index.html.erb file.
For the specific page where you want this js file add in your html file.
<%= javascript_include_tag "articles" %>
I found paloma: https://github.com/kbparagua/paloma
I develop new Rails application for about 2 months with this gem, and it provide quite effective way to manage javascript.

Rendering an HTML file in rails

I am using CarrierWave to upload static HTML templates to certain user profiles on my webpage.
I am trying to render the HTML file on the user's home page once they log in. The Path for the HTML file after uploading is:
/uploads/profile/curation/8/User_Content.html
I'm new to rails and I thought I'd just be able to
<%= render /uploads/profile/curation/8/User_Content.html %>
to get the html to render, but I guess that only works for partials, which this is not.
Any advice?
You can use render_to_string. Please have a look over here. http://russbrooks.com/2009/11/18/embed-a-static-hmtl-page-in-a-rails-view
In your controller you can redirect to the page you want:
redirect_to "/uploads/profile/curation/8/User_Content.html"

How to show images of links in profile

I think this is a simple problem but I do not know how to phrase it to get a sample solution from a search engine.
Instead of allowing profile image uploads/gravatars on my website I would like to store links to images in the profile db table and embed those images in the profile show page view.
My initial thought is just to have a string url filed in the db. From there I do not know how rails will parse the string url and render the image.
Any suggestions are greatly appreciated.
If you have a link to the image stored as a link on the profile, then in your view you could do something like this:
Add image_link to Profile model
In controller
def show
#profile = Profile.find(params[:id])
....
end
In view
<%= image_tag #profile.image_link %>

Creating a Rails partial that is model-agnostic

I have the two Ruby on Rails models SafetyTest and TeamDue, and want to create a partial called _seasonal.html.erb that can work with either models.
The real problem is adding a link to create a new instance. For example, it would be this code for just SafetyTest:
<%= link_to new_safety_test_path %>
Now I want to be able to specify in my view, when I'm rendering _seasonal.html.erb, whether I want such a link for SafetyTest or TeamDue . I'm not sure what local to pass to this partial so that it creates the right new link without making a mess.
How should I go about doing this?
Take a look at Polymorphic URL helpers.

Render a page as a saved HTML file using Rails 3.0?

I'm building a simple website generator application in Rails 3.0. I'd like a "publish" action in a controller that works just like an ordinary "show" action, but instead, saves a page as an HTML file in the "public" directory instead of displaying it in the browser. That is, I'd like to use the Rails render mechanism to create a file instead of providing an HTTP response.
What's the best way to do this?
Should I simply add caches_page :publish to the controller?
Or use render_to_string and File.new?
You can use render_to_string method: http://apidock.com/rails/AbstractController/Rendering/render_to_string
You still need to respond from the controller though. Maybe redirect to the page you just saved?
I would go with page caching.
Then if you have editable content, the pages should be automatically generated. You could then write a system task which bundles them up as a web site.
see (whatever)/actionpack/lib/action_controller/caching/pages.rb for instructions.
I found that page caching using caches_page won't work for me because there is no way to show a notification or redirect to another page after caching the page. The method render_to_string (suggested by #Grocery) is the way to go. Here's the example controller code:
def publish
#article = Article.find(params[:id])
html = render_to_string(:template => "articles/template.html.haml", :layout => 'article' )
FileUtils.makedirs("#{Rails.root}/public/articles/") unless File.exists?("#{Rails.root}/public/articles/")
File.open("#{Rails.root}/public/articles/#{#article.filename}.html", 'w') {|f| f.write(html) }
respond_to do |format|
format.html # publish.html.erb
end
end