有没有办法通过命令行添加到Rails的路由?

时间:2021-09-09 00:11:23

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?

我是轨道上红宝石的初学者。每次向控制器或项目添加新操作或页面时,我都发现手动将路由添加到route.rb非常不方便。所以我想知道是否有使用命令行而不是编辑route.rb文件的方法?

3 个解决方案

#1


Adding routes to the routes.rb file from the terminal can be easily achieved with sed.

使用sed可以轻松地从终端向routes.rb文件添加路由。

Install sed using the following command (Ubuntu):

使用以下命令安装sed(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:

这就是它的作用:

  1. File to add text to is config/routes.rb
  2. 要添加文本的文件是config / routes.rb

  3. Line number to insert text to is 23
  4. 插入文本的行号是23

  5. -i is the insert flag: the text will be inserted
  6. -i是插入标志:将插入文本

  7. resources :people is what gets added
  8. 资源:人才是被添加的东西

Now, the route resources :people will be inserted on line 23 in config/routes.rb file in your Rails app.

现在,路由资源:人们将在Rails应用程序的config / routes.rb文件的第23行插入。

#2


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

#3


If you do rails generate -h

如果你做rails生成-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.

如您所见,默认情况下没有路由生成器。这是铁轨用完魔术的地方之一。当然,您可以编写生成器创建和自定义Rails生成器和模板。

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.

但我建议,准备好在没有快速默认方式时编写代码。一旦你成为一名专业人士,你就会经常看到rails magic没有达到你想要实现的解决方案。

#1


Adding routes to the routes.rb file from the terminal can be easily achieved with sed.

使用sed可以轻松地从终端向routes.rb文件添加路由。

Install sed using the following command (Ubuntu):

使用以下命令安装sed(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:

这就是它的作用:

  1. File to add text to is config/routes.rb
  2. 要添加文本的文件是config / routes.rb

  3. Line number to insert text to is 23
  4. 插入文本的行号是23

  5. -i is the insert flag: the text will be inserted
  6. -i是插入标志:将插入文本

  7. resources :people is what gets added
  8. 资源:人才是被添加的东西

Now, the route resources :people will be inserted on line 23 in config/routes.rb file in your Rails app.

现在,路由资源:人们将在Rails应用程序的config / routes.rb文件的第23行插入。

#2


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

#3


If you do rails generate -h

如果你做rails生成-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.

如您所见,默认情况下没有路由生成器。这是铁轨用完魔术的地方之一。当然,您可以编写生成器创建和自定义Rails生成器和模板。

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.

但我建议,准备好在没有快速默认方式时编写代码。一旦你成为一名专业人士,你就会经常看到rails magic没有达到你想要实现的解决方案。