user3301669
user3301669

Reputation:

Ruby on Rails Routing error while accessing form my local browser

I have the following error while I am viewing my webpages in browser.

book is my directory in that edit.erb is my view page

route.rb

LibrarySystem::Application.routes.draw do
  get 'book#edit'
end

while I am accessing :3000/book/edit" it showing the following error

ActionController::RoutingError (No route matches [GET] "/book/edit"):
  actionpack (4.0.3) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (4.0.3) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.0.3) lib/rails/rack/logger.rb`enter code here`:38:in `call_app'
  railties (4.0.3) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.0.3) lib/active_support/tagged_logging.rb:67:in `block in tagged'
  activesupport (4.0.3) lib/active_support/tagged_logging.rb:25:in `tagged'
  activesupport (4.0.3) lib/active_support/tagged_logging.rb:67:in `tagged'
  railties (4.0.3) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.0.3) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  activesupport (4.0.3) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  actionpack (4.0.3) lib/action_dispatch/middleware/static.rb:64:in `call'
  rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
  railties (4.0.3) lib/rails/engine.rb:511:in `call'
  railties (4.0.3) lib/rails/application.rb:97:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  rack (1.5.2) lib/rack/content_length.rb:14:in `call'
  rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
  c:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
  c:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
  c:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'


  Rendered c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-4.0.3/lib/action_dispatch/middleware/templates/rescues/_trace.erb (0.0ms)
  Rendered c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-4.0.3/lib/action_dispatch/middleware/templates/routes/_table.html.erb (15.6ms)
  Rendered c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-4.0.3/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (62.5ms)

Upvotes: 0

Views: 154

Answers (2)

Kirti Thorat
Kirti Thorat

Reputation: 53038

Replace

LibrarySystem::Application.routes.draw do
  get 'book#edit'
end

WITH

LibrarySystem::Application.routes.draw do
  get 'book/edit'
end

You can define a shortcut route as get 'book/edit' which Rails can interpret correctly and map to book#edit plus create a named route as book_edit_path as mentioned below:

book_edit GET    /book/edit(.:format)   book#edit

Writing the route as get 'book#edit' would probably give you an error right at time you try to start the server or try to access a page(if it was already started):

ArgumentError (missing :controller):

That makes me believe that the scenario you shared is incorrect as defining your route as

get 'book#edit' would not result in

ActionController::RoutingError (No route matches [GET] "/book/edit").

Upvotes: 0

rails4guides.com
rails4guides.com

Reputation: 1441

Your route is not defined correctly, I would recommend you to read through the commented out part of your routes.rb file for examples.

Oh well, I'll expand my answer. In your comment I can see you have some basic CRUD routes. The easiest way to define these routes is the following:

resources :books

Now check rake routes to see what routes are generated. You might not need them all, but you can set limitations through only or except. Example:

resources :books, except: [:show]

Now if you want a custom route for an individual book, you can add a member to your resources, like:

resources :books do
  member do
    post 'upvote'
  end
end

Be aware though, if you want custom routes you should have an action with the same name, in this case upvote, defined in you controller.

If you want to set a route for a collection of books you can use collection instead of member. It is important to think about how your URLs should look like and if you want the route for either an individual book or a collection of books.

Calling routes for an individual book passes along the id of that book. Basic URL: ../book/1/edit. This is necessary for the controller, because you need the id params to work with. You can also make the URL more pretty by naming your route. Example:

get 'summary', to: 'books#show, as: :summary

Right, I can go on all day like this, but you should probably check the railsguides on this subject first.

Upvotes: 1

Related Questions