I've installed ruby on rails 3.2.6, and when I execute
rails server
and access to 127.0.0.1:3000 it works, however when I generate a controller, for example
rails generate controller principal
and access to 127.0.0.1:3000/somecontroller, browser show following error:
Routing Error
No route matches [GET] "/principal"
Try running rake routes for more information on available routes.
What do I need to do, and can this be simply explained?
The problem is you did not specify any actions, so your controller 'principal' is empty, no views will be created with similar names, and no routes created.
You need to do:
rails generate controller principal index [show] [edit] [update] [create] [destroy]
The name after your controller name are the action names. Since you said controller 'principal' in the singular, then it might imply that you have a singular resource. If you want to have it in the plural, make sure you say 'controller principals'.
And your routes should should show:
resource :principal [ or :principals or multiple Restful routes ]
You need to edit config/routes.rb to tell the router which controller to route your request to. The rails standard is to use RESTful routes and in the example you've given this would equate to a singular Principal resource. Therefore you'd need to add:
resource :principal
to generate a set of RESTful routes for this resource. You can see the routes generated by doing:
rake routes
If you don't care about REST then you can simply add (assuming the PrincipalController has an index method):
match 'principal' => 'principal_controller#index'
Have a look at this chapter from the Rails Guides for more info on routing:
http://guides.rubyonrails.org/routing.html
You can also generate methods in the controller and routes at the same time by supplying their names as arguments to the rails generate controller command for example:
rails generate controller principal index
Related
I'm really confused because some of my routes when I use controllername_index_url are routing to /controllername/index while other's are routing to /controllername.
The main difference is that when I generate the index method when I create the controller through the command line
rails g controller controllerName1 index
the route then becomes /controller_name_1/index.
When I create the index manually after creating the controller, it becomes /controllername2
In my config/routes I am including my controllers like:
Rails.application.routes.draw do
resources :controller_name_1
resources :controller_name_2
end
And when I do a rails routes my routes look like
controller_name_1_index GET /controller_name_1/index(.:format) controlle_name_1#index
controller_name_2_index GET /controller_name_2(.:format) controller_name_2#index
Why is it automatically adding the routes differently? How can I make it so that regardless of it I generate the index methods on the command line or add them in after the fact, controller_name_index_url routes are always the same format? (I'm using ruby 2.4.0 and rails 5.1.2 if that's helpful)
When you create an action at the command line a route is automatically generated for it.
$ rails g controller ones index
create app/controllers/ones_controller.rb
route get 'ones/index' # route helper is ones_index
...
The behavior is totally agnostic of the action you're creating. It could be any action name and Rails will do the same thing
$ rails g controller twos smorgas
create app/controllers/twos_controller.rb
route get 'twos/smorgas' # route helper is twos_smorgas
...
When you add resources to your routes
resources :ones
you automatically get all of the default REST route helpers and routes, no matter how you create any of the REST actions.
$ rake routes
ones GET /ones(.:format) ones#index
POST /ones(.:format) ones#create
new_one GET /ones/new(.:format) ones#new
edit_one GET /ones/:id/edit(.:format) ones#edit
one GET /ones/:id(.:format) ones#show
PATCH /ones/:id(.:format) ones#update
PUT /ones/:id(.:format) ones#update
DELETE /ones/:id(.:format) ones#destroy
It's best to stick with Rails convention and use the default route helpers
ones_url
ones_path
# not ones_index_url
See this SO question if you want to disable the automatic route generation
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.
I am new to Ruby on Rails and have some problems.
For the development I use RubyMine IDE, I manage to create models, controllers and views, but I have problems with the routing. By default, routes.rb file contains only this method Apis::Application.routes.draw do with an empty body.
For example, I create a controller TestController, then the index method and in routes.rb I add this instruction resources :test. So far, it works fine. But if I add another method, let's say method1 (and the view) I can't reach it in a browser http://localhost:3000/test/method1.
What else should I add in routes.rb file?
Is there any way to make the routing automatically from the IDE, with less editing the routes file?
resources :test
is a resourceful route which provides a mapping between HTTP verbs and URLs to controller actions. By convention, each action also maps to particular CRUD operations in a database
you can uncomment in your routes to enable the controller action mapping.
match ':controller(/:action(/:id(.:format)))'
or use -
match "/test/method1" => "test#method1"
Detailed routes info # http://guides.rubyonrails.org/routing.html
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.
I am new to Rails and I am reading Sitepoint's Simply Rails 2 but I am using Rails 3.0.0. In the book we had just set up our first controller (StoriesController) and fired up the server and typed in http://localhost:3000/stories and it should have displayed the index.html.erb file but instead when I type in that url I get "Routing Error: No route matches "/stories" but when I type in http://localhost:3000/stories/index it works properly. Can somebody explain to me why rails is not loading the index.html.erb file implicitly when I go to localhost/stories?
Depending on how you created your routes (in config\routes.rb). Unfortunately, if you scaffold a controller, rails now generates a route like this:
get 'posts#index'
If it is a restful-controller, you better write
resources :posts
Or if it is a special controller (with only an index action) you could write
match '/posts' => 'posts#index'
To provide the fallback match ':controller(/action(/:id(.:format))) is generally avoided. Because it opens up all your controller-methods. The preferred way is that you declare explicitly how to access your site.