Rails routes to documentation - ruby-on-rails

I use rails 4 for a restFUL API and would like to use http://apidocjs.com/ as i did with php.
I could generate my api documentation in /doc but after I'm wondering what is the best "rails way" to route to this doc. Should I create a controller or just routes to my html file like :
get '/doc', :to => redirect('/doc/index.html')
I tried it but I get
No route matches [GET] "/doc/index.html"
So what is the best way to do that ? I feel like I don't think "rails way"..

If your documentation is completely generated and just static html, you can simply place it within your public folder and it will be routed automatically. In other words, you can create the docs folder within the public folder and then access your pages via
http://example.com/docs/index.html
In development this would be
http://localhost:3000/docs/index.html
If you're looking for something more robust, I'd highly recommend high_voltage by thoughtbot.

You could try like this provided you have controller named as docs and action named as index.
get 'docs', :to => 'docs#index', :as => 'doc'

resource :doc, only: :show
Then create this file
/app/views/docs/show.html
No controller needed
The URL will be
/doc

Related

Insert routes programmatically in Rails

I'm creating an engine that needs to insert some routes into the application's router. For this particular gem, I'd rather not application's routes.rb if possible. Is there a way to insert routes at a particular location in the router via code? I'm looking for an API that does something like:
Rails.application.routes.insert("resources :foos", :before => "some string in routes.rb")
If I create a config/routes.rb inside the engine and define some routes, this kind of works. Rails is smart enough to mix the engine's routes into the application's routes, but it tacks them on at the end of the route list. I need them to appear at the beginning so the engine's routes take priority.
I'm aware that I can namespace the routes by mounting the engine in the application's routes.rb, but this creates a routing structure that I don't really want. I want the engine's routes to look they are a part of the application by defining some routes in the actual application.
I have a workaround which is to add the following to the application's routes.rb.
Rails.application.routes.draw do
MyEngine.setup_routes(self)
#...other routes below
end
MyEngine.setup_routes looks like
def self.setup_routes(map)
map.get 'a_path', :to => 'a_controller#a_path'
end
This at least allows me to control the point where the routes get defined in the application's route list, but the user still has to manually update his routes.rb (or I have to build an installer that does it). It seems like there should be a way to tell rails to tack some routes onto the start of the route list...

Custom URL routes helper

It seems there is no URL helper generated automatically when you apply custom.
So below is what is defined in my routes.rb file but when I do rake routes I don't have any URL helper generated at all.
get "events/display_event/:id", :to => "events#display_event"
It seems it is generated only for resources in the routes.
Is there a way to generate this automatically or do I have to manually specify without using the helper?
If it has to be done manually in the view, what is the best approach for this?
This should do it.
get "events/display_event/:id" => "events#display_event", as: :display_event

How to change routes in ruby on rails?

I just installed Ruby on Rails and created a scaffold called posts. RoR generated controllers and other required files for me.
I created a new method in posts_controller, but I can't access it. I looked at other methods that are in the controller and looks like I need to access them by /posts/[MY POST ID]/[MY METHOD NAME].
Assuming I created my custom method hello in the controller, how do i access it?
I looked at routes.rb, but there's no configuration for it.
Updated:
I understand that I can manually configure it in routes.rb, but how do all the other methods work? For example, I have "edit", and "update" methods in the "posts_controller.rb" controller. How do those two methods work without configuring routes?
# GET /posts/1/edit
def edit
#post = Post.find(params[:id])
end
I can't find a configuration that matches /posts/[0-9]/edit pattern.
The documentation you're looking for is Rails Routing From the Outside In. Once you've read this you'll understand everything Rails does to take your request and point it at method in your controller.
You need to add a route for it to routes.rb. For example:
# Previous routes
# resources :posts
# Updated routes
resources :posts do
get "hello", :on => :member
end
Have a look at this Rails guide about routing, it should help you understand Rails routing.
This will give you a good head start on the routes: http://guides.rubyonrails.org/routing.html
Not every method you make will have its own path, rails is built on the rest principle, and your scaffold created methods in the post controller that follow those paths, like index, show etc....
You can force your method to have a route added to it, but in reality you rarely actually need to do so as following the convention is far easier.
In Rails 3.x
match 'posts/hello' => 'posts#hello'
Available at example.com/posts/hello
When you used scaffold to generate post, it added a line resources :posts in your routes.rb file. That line configures routes for all the controller actions that were generated. As Caleb mentions above, not every action has a dedicated path. A single path can correspond to multiple actions because rails also takes into account the HTTP method. So, for instance, the path /posts with the HTTP method GET corresponds to the controller action index, while the same path with the HTTP method PUT corresponds to the controller action update. You can see these associations when you run rake routes from the console. I agree with Jordan and Caleb that the Rails Guides is a good read and will help you understand routes.

Confusion routing Ruby requests

I am following this routing tutorial for Ruby on Rails:
http://guides.rubyonrails.org/routing.html
It says that when I need to create a new url, I should make a route for it. So I did that.
I would like to have a url like this www.domain.com/fomats/formats.html.rb so I did something like this in the routes.rb file:
resources :formats
get "formats/index" #display all formats
Is that correct? For my index route, I also have something like this in my route.rb file: root :to => "home#index" - should I have something like this in the formats route?
Also, how do I create the actual controller? Do I make it by hand, or does rails somehow create the stub of it for me?
Right now I get this error:
missing :action
Does that mean I am missing the controller or something else?
Thanks,
Alex
As others have said, you should probably continue studying with other books or resources. These fundamental questions you are asking may become more clear the more you read.
Here are some quickfire hints that hopefully help you.
---
When you declare this in the routes.rb file:
resources :formats
You automatically get the following declaration for free, so you don't have to re-declare it:
get "formats/index" # Don't add this to routes.rb
---
URL's in rails look like this:
www.domain.com/formats
That URL would map to "formats#index"
---
To see what explicit routes have been generated, run this in your rails root directory:
rake routes
---
To create a controller:
rails g controller formats

Generating an object's absolute url without html markup

Is there a method like "full_url" such that #comment.full_url or full_url_for(#comment) returns "http://www.host.com/comments/id" where www.host.com is the default host domain and id is #comment.id. Or, if not, what would be an elegant way to generate this url string?
I'm pretty new at Rails, most of the methods I've learned insert the tag and other markup.
url_for is not helping because I can't do something like the following:
url_for(#comment, {:only_path => false})
I've spent way too much time trying to figure this out. It came down to either hacking or asking for the right way on SO. Here I am.
If you are setting up your routes correctly in your config/routes.rb file then you should have access to named routes in your controller and in your views. Which should mean that all you should need to do is:
comment_path(#comment)
Or for the full url
comment_url(#comment)
To see a list of all of the routes from the command line, you can type rake routes from the project root. Welcome to rails! Here is a good resource for rails 3 routing: http://guides.rubyonrails.org/routing.html
some additional resources via Railscasts:
http://railscasts.com/episodes/231-routing-walkthrough
http://railscasts.com/episodes/232-routing-walkthrough-part-2

Resources