Routing pages in Ruby on Rails - ruby-on-rails

I am new in RoR. I just added a new page hello.html in public folder. When I try to load the page on browser I get following error
No route matches [GET] "/hello.html"
Try running rake routes for more information on available routes.
I found some solution so far but they don't really working for me. I tried following lines in routes.rb file
match '/hello', to: '#hello'
match 'hello', to: 'hello'
match '/hello' => '#hello', :as => 'hello'
But Im getting same error. I know that for files in public folder I don't need to add routes but Still I get the same error.

For any files added in the public folder, you don't need to add routes. If the file is available as
public/hello.html
Its accessible via localhost:3000/hello.html
Simple! No routes.

Related

New rails installation with error when entering url

SOLVED In the /etc/apache2/vhosts.d/default.conf we inserted the following
RewriteEngine On
RewriteRule "^/?$" "http://ourpage.com/projects"
--------------------------------------------------------------------------------------------------------------
We installed rails on a new server. When we try access the url we get redirected to ourstuff/public which is not desired. In ourstuff there is app, config, public etc.
EDIT:
Could it be that the mod passenger does not work properly?
The following from public is displayed in the browser (see scrshot). My collegaue said its from the the documentary root from the apache config file.
public
When we try the usual url xyz.com/ourstuff like from our other systems we get this error
Routing Error
No route matches [GET] "/ourstuff"
Try running rake routes for more information on available routes.
Does anyone have an idea why this is?
Thank you in adcvance.
Have you set up your routes?
In your config/routes.rb file add:
get '/ourstuff', to: 'controller#action', as: 'ourstuff'
This means when your server receives a get request to yourapp.com/ourstuff, it will send the request to the controller and action you specify.
It's then up to you to put in the required logic in that action, and present the relevant template. See:
http://guides.rubyonrails.org/routing.html
EDIT: You need to add this line inbetween:
myqpp::Application.routes.draw do
and the final
end
on its own line. i.e.
myqpp::Application.routes.draw do
root :to => 'projects#index'
get '/ourstuff', to: 'controller#action', as: 'ourstuff'
end
Once more, you will need to set up a controller, an action for that controller with the necessary logic, and a template to render at the end.

Rails routes to documentation

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

Rails routing error when using resource but now when using GET

I am creating a simple suggestion box app (to learn Rails) and am getting the following Rails routing error when I go to "/suggestion-boxes" running on my local machine (localhost:3000)
"Routing Error
No route matches [GET] "/suggestion-boxes"
In my routes.rb file I have:
SuggestionBoxApp::Application.routes.draw do
resources :suggestion_boxes
end
This is what I get when I run rake routes:
suggestion-box-app$ rake routes
suggestion_boxes GET /suggestion_boxes(.:format) suggestion_boxes#index
POST /suggestion_boxes(.:format) suggestion_boxes#create
new_suggestion_box GET /suggestion_boxes/new(.:format) suggestion_boxes#new
edit_suggestion_box GET /suggestion_boxes/:id/edit(.:format) suggestion_boxes#edit
suggestion_box GET /suggestion_boxes/:id(.:format) suggestion_boxes#show
PUT /suggestion_boxes/:id(.:format) suggestion_boxes#update
DELETE /suggestion_boxes/:id(.:format) suggestion_boxes#destroy
However, if I modify my routes file to
SuggestionBoxApp::Application.routes.draw do
get "suggestion-boxes" => "suggestion_boxes#index"
end
Then the page "/suggestion-boxes" displays as per the index action in my SuggestionBoxesController.
I tried restarting my server but this had no impact. While I of course can go with using GET, this error makes no sense, and I would like understand what is causing it.
Any insights would be very much appreciated.
The error is that you are not renaming the REST route, instead the controller one.
Try declaring
resources :suggestion_boxes, :as => "suggestion-boxes"
in your config/routes.rb file.
Somewhere in your code you are calling to suggestion-boxes controller which does not exist. Your controller is suggestion_boxes, spelling. So where every you have "suggestion-boxes" you should replace with "suggestion_boxes". The code that you added create an alias that matches 'suggestion-boxes' to the index action of the suggestion_boxes controller so this resolves it if it is your desired affect. But simply fixing your spelling would resolve your problem. I usually use the second approach if I want the change the URL that user see. Have a look at the routing doc for a better understanding

catching wild card route for missing routes, doesn't work when having additional routes files

I implemented a simple custom errors solution.
this one: http://ramblinglabs.com/blog/2012/01/rails-3-1-adding-custom-404-and-500-error-pages
everyhing is working fine except the missing routes in the routes.rb file..
in order to get to my error_controller when there is a missing route i did the wildcard solution: match '*not_found', to: 'errors#error_404'
but... now when i try to enter a sub section of my site which seats under:
/admin, i get to the error page. the wilcard gets triggered, even tough the route for admin section is defined in a different route file, under: config/routes/admin.rb
what can I do?
thanks
edit:
using rails 3.0.20 and ruby 1.8.7
If you're using Rails 3.2+, there is a simpler solution for your routes. First in 'config/application.rb' set your app as the error handler
config.exceptions_app = self.routes
Now when there is an your app will look to your routes to handle it. In 'config/routes.rb' you can add a route such as:
match "/404", :to => "errors#not_found"
A more verbose explanation can be found here.
OK so until I will update to Rails 3.2+
I simply put '*not_found', to: 'errors#error_404' into the last route file that is loaded.
that way its truly in the end of the routes and now all my routes work. and the error is still fired when needed.

Whats wrong in my first Ruby On Rails application?

I'm new in Ruby On Rails. I installed in 2 days ago on windows and now I want to create a simple "Hello Rails" with it. I use This Tutorial. I did all the steps described in this it does'nt work. the steps are:
write this command in PowerShell: rails generate controller home index
Open app/views/home/index.html.erb in text editor and edit it to contain a single line
of code: Hello, Rails!
delete the default page from application with this command
rm public/index.html
Open the file config/routes.rb in editor, and edit root
:to => "welcome#index"
to
:to => "home#index"
navigate to
"http://localhost:3000"
in my browser but I see this error instead Hello Rails:
(I don't have enough reputation to post image of error)
Routing Error
No route matches [GET] "/"
Can anybody tell me whats the problem?
Thanks
Make sure the only content in your config/routes.rb file is the following:
Blog::Application.routes.draw do
root :to => "home#index"
end
(just delete everything else for now to make sure it's clean)
Also, try killing the server and making sure that you don't still see that error message (which could indicate that another server is still running somewhere). Along these lines, make sure you see the request in your console output when you fetch the page.
If you're still getting the error, look for any clues in the console error messages.

Resources