I am a beginner in ruby on rails. I found it quite inconvenience to add route to route.rb manually every time I add a new action or page to controller or the project. So I want to know if there is a way using command line rather than editing the route.rb file?
Adding routes to the routes.rb file from the terminal can be easily achieved with sed.
Install sed using the following command (Ubuntu):
sudo apt get install sed
Assuming you are in the root directory of your app, here is the command to add the routes:-
sed -i '23iresources :people' config/routes.rb
This is what it does:
File to add text to is config/routes.rb
Line number to insert text to is 23
-i is the insert flag: the text will be inserted
resources :people is what gets added
Now, the route resources :people will be inserted on line 23 in config/routes.rb file in your Rails app.
I don't know about a commandline way to update routes, but you might consider using wildcards instead, that way a single line in your routes file can allow you to access many pages on your site:
http://guides.rubyonrails.org/routing.html#route-globbing-and-wildcard-segments
If you do rails generate -h
rails generate -h
Please choose a generator below.
Rails:
assets
controller
generator
helper
integration_test
jbuilder
job
mailer
migration
model
resource
scaffold
scaffold_controller
task
As you can see there is no generator for routes by default. This is one of the points where rails runs out of its Magic. Of course you can write your generator Creating and Customizing Rails Generators & Templates.
But I would recommend, be ready to write code when there isn't a quick default way. Once you become a pro you will quite often see rails magic falling short of the solution your are trying to implement.
Related
I'm definitely very new to Ruby on Rails. I created a new rails folder like this:
Edit: Sorry for the inconvienience, but I did use the --api tag.
rails new <project-name> -d mysql --api
since I use mysql instead of sqlite. Then, in the tutorial they used a command like this:
rails generate controller Welcome index
I did the exact same thing, however in the log it shows this:
create app/controllers/welcome_controller.rb
route get 'welcome/index'
It didn't seem to create the view. I don't know why it's doing this. After I start the server with rails server I can go to the localhost:3000/welcome/index but it just takes me back to the home page, and in the log it says Started GET .... No Content ... . So it couldn't find the view. How can I fix this?
Unless you created an API only project with the --api flag, the rails generate controller Welcome index command should have created an index.html.erb file in the views folder.
You can either delete the welcome_controller.rb file and the generated line from the route.rb file and run the command again or you can manually create the view with the touch command (assuming your OS supports the touch command):
touch app/views/welcome/index.html.erb
or through you favourite text editor or IDE. On a side note, controller names should be plural (WelcomePagesController for example).
I am dealing with a Rails project that has a huge and complex list of routes. When I run rails routes I can see the route I want but when I look at the files that define the routes I am unable to find it. Is there any method where given the name of the route from rails routes it can give me a line number showing where that route was defined?
For Rails < 5
You can use
rails routes | grep yourSearchString
For Rails >= 5
You can use option -c to search for routes related to controllers
rails routes -c users
will give you routes of UsersController
You can use -g option to do general purpose pattern matching
rails routes -g users
will give all the routes that partially or completely matches with your search
Hope it helps :)
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 want to list all defined helper path functions (that are created from routes) in my rails 3 application, if that is possible.
Thanks,
rake routes
or
bundle exec rake routes
Update
I later found that, there is an official way to see all the routes, by going to http://localhost:3000/rails/info/routes. Official docs: https://guides.rubyonrails.org/routing.html#listing-existing-routes
Though, it may be late, But I love the error page which displays all the routes. I usually try to go at /routes (or some bogus) path directly from the browser. Rails server automatically gives me a routing error page as well as all the routes and paths defined. That was very helpful :)
So, Just go to http://localhost:3000/routes
One more solution is
Rails.application.routes.routes
http://hackingoff.com/blog/generate-rails-sitemap-from-routes/
rails routes | grep <specific resource name>
displays resource specific routes, if it is a pretty long list of routes.
Trying http://0.0.0.0:3000/routes on a Rails 5 API app (i.e.: JSON-only oriented) will (as of Rails beta 3) return
{"status":404,"error":"Not Found","exception":"#>
<ActionController::RoutingError:...
However, http://0.0.0.0:3000/rails/info/routes will render a nice, simple HTML page with routes.
CLI
updated for version 6
To list all existing routes you'll want to run the command:
bundle exec rails routes
bundle exec will
Execute a command in the context of the bundle
and rails routes will
list all of your defined routes
Example
If you have your resource route declared like so:
resource :credential, only: [:new, :create, :destroy]
Then it's helpful to pipe the output so you can grep for your specific resource.
For example:
I know this is a minor issue, but why, if you use scaffolding in RoR, can you use lines like 'new_model name here_path' in link tags, but without using scaffolding, I get a NameError? For example, I have a simple address book app that uses basic CRUD operations. I am a RoR beginner but wanted to build an app without scaffolding and these kind of things don't seem to work. I compared my config/routes.rb and app/helpers/* with those in a scaffolded app and they are no different. What am I missing?
One way to check your routes and paths is to run:
rake routes
It outputs all your routes and paths.
Scaffolding sets up resource routes in the routes.rb file. The resource routes are what give you the path and url helpers. When you don't use scaffolding the routes aren't added, you must do it by hand.
Resource Routes can be added like so:
map.resources :models
where :models is the plural name of one of your models.