How do you render the internal code documentation within a rails project - ruby-on-rails

How do you make the documentation available after running rake doc:app available within your project.
I know I can browse on the local machine but I want to allow other uses to browse the documentation within the application.
I have created a documentation controller within which I have an index method as follows
class DocumentationController < ApplicationController
def index
render 'doc/app/index.html'
end
end
This partially works as it renders the frame but each frame is filled with messages such as
No route matches "/files/doc/README_FOR_APP.html" with {:method=>:get}
So how do I change where a particular controller sets its equivalent of DOCROOT to.

Why don't you copy 'doc' into the 'public' directory in the root of your rails app? They are just static HTML files and would be available through just Apache serving them.

Related

How to list directory folders in rails

Hi I am newbie in rails and wanted to know any way to list folders and files for a url say public . So example user types example.com/public he gets all folders and files listed as we do in php if we start a wamp server . I need this so I can create files and have their url shared to public i.e. simply send url link online like example.com/public/test.pdf . Currently I am getting page with no routes defined .enter image description here
Thanks.
Create a controller to serve the file using a route parameter, ex.
get '/public/:filename', to: 'file#serve'
Then use the send_file method in your controller.
class FileController < ApplicationController
def serve
# For authorization:
#authorize!
# For authentication: (a way or another)
#redirect_to :access_denied unless user_signed_in?
send_file "/www/yourapp/var/files/#{params[:filename]}"
end
end
This way the file can be anywhere within your app, or even on a Cloud storage. It also gives you the ability to use authentication or authorization to check if the user has access.
Please note that this code is very simple, but there are much more options like Fog gem for Cloud storage and everything else.
See https://apidock.com/rails/ActionController/Streaming/send_file for more information.

Allow users to download a file

I've read through a number of StackOverflow threads and tutorials, and I haven't found a good, simple explanation for how to allow a user to download a file from your site.
All I want to do is add a link to one of my views which downloads a file when clicked.
I'd like to understand:
Where do I store the downloadable file in my file system? public?
Is there anything special about linking to the file in your view, or it's just a link_to?
What needs to happen in routes? It's just a get for that controller#action?
What needs to happen in the controller? In rails documentation I've read that you need to "be careful to sanitize the path parameter if it is coming from a web page," but I'm not sure exactly what that means.
Thanks!
In simple scenario, you don't need controller to download file. Just save file to public folder. Public folder is default folder for static resources there are stored compiled js, css, images files, robot.txt and so on.
If you have file monthly-report.doc. Put it to public/reports/monthly-report.doc.
In view link_to 'Donwload Report', '/reports/monthly-report.doc'
There are basically two cases:
1. The file is public and should be downloadable by anyone.
Place it in the /public directory. Remember that this is the web root - so if you have a file that lives at /public/foo/bar.baz you would link to the file with <%= link_to 'A file', '/foo/bar.baz' %>.
No routes or controllers are need since we are just serving a static file from the servers public directory.
2. The file needs access control
In this example we are dynamically servering files stored in /downloads.
# routes.rb
resources :downloads, only: [:show]
class DownloadsController < ApplicationController
# do your authentication logic here
# GET /downloads/:id
# #example
# GET /downloads/foo.bar would download a file stored at
# /downloads/foo.bar
# #raise [ActiveRecord::RecordNotFound] if the file does not exist.
# This causes a 404 page to be rendered.
def show
fn = Rails.root.join('downloads', params[:id])
raise ActiveRecord::RecordNotFound and return unless file.exists?(fn)
send_file(fn)
end
end
By using Rails to serve the download we can apply whatever access control rules we want.
The link_to is the same as any other link.
If you want to store it in public then do this in whatever controller action you want.
send_file File.join(Rails.root, 'public', 'file.extension')
You could create a downloads controller and specify that in the index then simply link_to 'Download', download_index_path or such.
If you're trying to send a file name that a user inputs, you have to sanitize it. If it's "hard coded" like the above example, then you're fine.

Ruby on Rails, Routing

Just wanted to know, what does this line mean in the routes.rb file:
AppName::Application.routes.draw do
Please explain. I am new to Rails.
Have a read through this page.
Basically, within the block passed to Application.routes.draw (which is just a call to a method defined in ActionDispatch::Routing module within the Rails core framework), you define all the URLs/Paths that you want your Rails application to respond to.
You can see all these route definitions, by running:
rake routes
in your terminal.
It is the main routes file which defines the root and other paths for the link.
It is used as suppose you want to change your index page from default ruby on rails to your index page you make changes to file and add
root to: "controllername#index"
This file is also used to add the model to the application
resources: "model_name"
Apart from this you can also define links in your rails application
get 'courses/index'
So going from courses controller to view of the index.

Rails file location and access

My root is site/home/ubuntu/workspace/
Is it possible to access file (from browser by HTTP request) located inside workspace/ without configuring routes and controllers?
Does the question on 1) depend on file extension?
you can access path to your file like this:
File.expand_path("somestuff.rb", "~/workspace")
for me this code produces path as follows:
"/home/foodie/workspace/somestuff.rb"
I have this structure:
# /home/username/Workspace/rails_project/app/controllers/application_controller.rb
require "#{Rails.root}/../test.rb
And
# /home/username/Workspace/test.rb
# Some ruby code
As you can see, test.rbfile is outside RubyOnRails Project.
For security reason, you can not access a file outside project rails from URL without defining a route. What you can do, is point a route to controller that, based on file name provided at url, require a file outside rails project.
# /home/username/Workspace/rails_project/config/routes.rb
get '/get_file/:file_name', to: 'files#show'
# /home/username/Workspace/rails_project/app/controllers/files_controller.rb
class FilesController < ApplicationController
def show
document = params[:file_name]
send_data "#{Rails.root}/../#{document}, filename: document
end
end
For more info, see:
http://api.rubyonrails.org/classes/ActionController/DataStreaming.html#method-i-send_data
Without configuring routes and controllers, client can only access files in the public/ directory (it doesn't matter what the extension is). Bare in mind this: when your Rails app is run by webserver, its webroot will be the public directory, consequently to access public/file.ext you request should be webroot/file.ext

Rails 3.1 Mountable Engines : How to use/template it inside another application?

Let's say I created a mountable engine called 'Soho' that has a controller for 'Users'. I can go to /users/1 to see my user with ID 1.
Inside 'Soho', I have a application.html.erb for layout.
Now let's assume I want to "blend" my engine 'Soho' in an application called 'Soho_test', and I mount my engine at "/". So, in my host application 'Soho_test', I can also go at /users/1 to see my user with ID 1. This is working.
My question is : how can I do in my host application 'Soho_test' to apply the 'Soho_test' application.html.erb to the /users/1 (user profile page) instead of the one in the 'Soho' mountable engine?
Thanks!
I found how to achieve it, so I will post my answer on my own question, in case anyone else wonders. It is actually quite easy. I should have thought about this in the first place...
All you have to do is to create a folder in your /views/layouts/ with the name of your engine. So according to my question, it would be /views/layouts/soho/. In that folder, put the application.html.erb that you want to have.
You can do the same with partials and other views. Just create a folder /views/soho/.../ and put your files there. I havn't found a rake task to copy the engine views in my host application, so I wrote one.
After reading your question over a few times, I think all you are trying to do is override a layout for a given controller.
If that is the case just specify the layout to use within your controller see the section 2.2.13.1 Specifying Layouts on a per-Controller Basis within the Rails Guide for Layouts
Here's an example:
class UsersController < ApplicationController
layout "users"
#...
end

Resources