error in Index.html.erb - ruby-on-rails

When I run the rails server it returns with an error in index_html.erb. the code ran great for a few minutes then I came across this error.
routes.rb file
[
[

Rails uses the routes.rb file to locate the various resources of your application, so any resource you expect your users to access will have to be defined in routes.rb. You don't have a route defined for home_about_path.
From your code, I take it you have an about method in your home_controller.rb controller file. You have to have the following line in your routes.rb (add it under 'get home#index'):
get 'home#about' => 'home/about'
Adding this line should create a home_about_path and your link_to method should work.
Understanding routing is incredibly important to Rails development, so you should do some reading and make sure you have a good grasp of the fundamentals before pushing too far ahead.

Related

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.

Using Ruby on Rails link_to to link to controller action

I'd just started toying around with Ruby on Rails and had come across an issue with linking to another action in a controller from a particular view. I am almost certain it's an issue (or lack of code) in my routes.rb file, but I think I'm misunderstanding exactly how this file works & what I have to do. I've got a solution but pretty sure it's not the "best way" to do it.
I have one controller called home with two actions, index (which is the default) and newbill. Inside index.html.erb I have:
<h1>Home View</h1>
<%= link_to "new", :controller => "home", :action => "newbill" %>
However I was getting a routing error:
No route matches {:controller=>"home", :action=>"newbill"}
Doing rake routes gives me the following:
root / {:controller=>"home", :action=>"index"}
I then (following some Googling) added this code to routes.rb
match 'home/newbill' => 'home#newbill', :as => :newbill
And then in my index.html.erb I've got this:
<%= link_to "Name", newbill_path %>
And now this works as expected. My questions however are:
Why does this work? What exactly is going on behind the scenes?
Surely this is not the best way to do it? Adding another match 'home/newbill'... for every controller / action I want to link to seems a rubbish way of doing things.
I really like Ruby, but struggling a bit with this aspect of Rails...routing in general is messing up my head a bit I think!
Any help is much appreciated :D
Thanks,
Jack
I guess the first time your code didn't work because your home controller is defined as a resource.
If you define a controller as a resource in routes.rb file it will support only 7 standard methods (according to REST architecture):
index
new
create
show
edit
update
destroy
If you need any more custom routes you should add them manually, say in your case 'newbill', may go as:
resources :home do
collection do
get :newbill
end
end
But as per my understanding, your newbill method should go to bills controllers new, method not in the home controller.
You are right, Rails routes are little bit confusing (at least for me), but once you understand you can do lots of cool stuff.
Read here for the Rails official routes documentation:
http://guides.rubyonrails.org/routing.html.
You should check out the Rails Routing guide. A read through will help you understand what is going on behind the scenes.
This works becuase rails filters every request through the router looking for a match. This enables you to define custom routes such as domain.com/post when the path is actually blog#post. Prior to rails 3, a catch-all route was the last route in the routes file. This allowed you to define a controller and action and it would just work. I'm on my iPad and not near any projects, so I can't verify it, but I think that route is still there in rails 3.1, it just needs to be umcommented.

Beginner RoR Error After Generating Views (RailsSpace)

I'm using RailsSpace to learn Ruby on Rails and am coming across an error after performing what seems like a simple command.
I used the Terminal to generate a new User controller with the views Index and Register:
$ rails generate controller User index register
And it had no problem with that, creating the files index.html.erb and register.html.erb as well as all the other expected files.
But when I visit http://localhost:3000/user/register, it comes back with the error message:
ROUTING ERROR: No route matches {:controller=>"user",
:action=>"about"}
My routes.rb doesn't indicate any abnormalities:
RailsSpace::Application.routes.draw do
get "user/index"
get "user/register"
get "site/index"
get "site/about"
get "site/help"
root :to => "site#index"
end
Why does it try to route to the "About" action, and what other file can I edit to change this routing?
I'm using Rails 3 in case that matters.
I would try hand coding in the route. In your case it would look like this:
match '/user/register' => 'users#register', :as => :register
This will definitely work and prevent the page /user/register from going to the about page. Let me know how things go and Ill try to continue to steer you in the right direction.

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

Resources