如何避免 Rails 脚手架将模型放入命名空间

发布于 2024-09-24 22:30:08 字数 156 浏览 0 评论 0原文

Rails 3 脚手架生成器将模型类放置在命名空间内。 示例:

rails generate scaffold admin/portfolio

但我只想将控制器和视图放置在管理命名空间内。

我怎样才能避免这种情况?

Rails 3 scaffold generator places model classes inside namespace.
Example:

rails generate scaffold admin/portfolio

But I want only controllers and views to be placed inside admin namespace.

How can I avoid that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

脸赞 2024-10-01 22:30:08

Rails 4 生成器有点不同。如果您使用scaffold_controller生成器,它将预先构建所有视图文件,但默认情况下它将查找名为Admin::Portfolio的模型。要加载正确的模型,只需将模型名称作为参数传递给生成器。

$ rails g model Portfolio
      invoke  active_record
      create    db/migrate/20150822164921_create_portfolios.rb
      create    app/models/portfolio.rb
      invoke    test_unit
      create      test/models/portfolio_test.rb
      create      test/fixtures/portfolios.yml

$ rails g scaffold_controller Admin::Portfolio --model-name=Portfolio
      create  app/controllers/admin/portfolios_controller.rb
      invoke  haml
      create    app/views/admin/portfolios
      create    app/views/admin/portfolios/index.html.haml
      create    app/views/admin/portfolios/edit.html.haml
      create    app/views/admin/portfolios/show.html.haml
      create    app/views/admin/portfolios/new.html.haml
      create    app/views/admin/portfolios/_form.html.haml
      invoke  test_unit
      create    test/controllers/admin/portfolios_controller_test.rb
      invoke  helper
      create    app/helpers/admin/portfolios_helper.rb
      invoke    test_unit
      invoke  jbuilder
      create    app/views/admin/portfolios
      create    app/views/admin/portfolios/index.json.jbuilder
      create    app/views/admin/portfolios/show.json.jbuilder

这将为您提供一个命名空间控制器和引用非命名空间模型的视图。

Rails 4 generators are a bit different. If you use the scaffold_controller generator it will pre-build all the view files, but by default it will look for a model called Admin::Portfolio. To load the correct model just pass the model name as an argument to the generator.

$ rails g model Portfolio
      invoke  active_record
      create    db/migrate/20150822164921_create_portfolios.rb
      create    app/models/portfolio.rb
      invoke    test_unit
      create      test/models/portfolio_test.rb
      create      test/fixtures/portfolios.yml

$ rails g scaffold_controller Admin::Portfolio --model-name=Portfolio
      create  app/controllers/admin/portfolios_controller.rb
      invoke  haml
      create    app/views/admin/portfolios
      create    app/views/admin/portfolios/index.html.haml
      create    app/views/admin/portfolios/edit.html.haml
      create    app/views/admin/portfolios/show.html.haml
      create    app/views/admin/portfolios/new.html.haml
      create    app/views/admin/portfolios/_form.html.haml
      invoke  test_unit
      create    test/controllers/admin/portfolios_controller_test.rb
      invoke  helper
      create    app/helpers/admin/portfolios_helper.rb
      invoke    test_unit
      invoke  jbuilder
      create    app/views/admin/portfolios
      create    app/views/admin/portfolios/index.json.jbuilder
      create    app/views/admin/portfolios/show.json.jbuilder

This will give you a namespaced controller and views that reference the non-namespaced model.

美煞众生 2024-10-01 22:30:08

rails 生成模型组合

rails 生成控制器 Admin::Portfolio

rails generate model Portfolio

rails generate controller Admin::Portfolios

猫七 2024-10-01 22:30:08

@RubyDev 建议 Ryan Bate 的 Nifty Generators 是正确的,但我不知道他为什么说使用 --skip - 型号选项。

Nifty Generators 实际上会完全执行您所要求的操作。只需将其添加到您的 Gemfile:

gem "nifty-generators"

并运行:

rails g nifty:scaffold Admin::Portfolio name:string

这将在“admin”命名空间中创建普通支架的所有内容,其中包含控制器和视图,但模型不在命名空间中。

@RubyDev was right to suggest Ryan Bate's Nifty Generators, but I don't know why he said to use the --skip-model option.

Nifty Generators will actually do exactly what you are asking for. Simply add it to your Gemfile:

gem "nifty-generators"

and run:

rails g nifty:scaffold Admin::Portfolio name:string

This will create everything a normal scaffold would with the controllers and views in an 'admin' namespace, but the model not in namespace.

倚栏听风 2024-10-01 22:30:08

按照 @tybro0103 更新

使用 nifty:generators: https://github.com/ryanb/nifty-generators

rails generate nifty:scaffold Admin::Portfolio

如果您已经生成了没有命名空间的模型或脚手架,并且想为管理命名空间再次生成,则可以跳过模型:

rails generate nifty:scaffold Admin::Portfolio --skip-model

如果您希望脚手架生成包含所有字段的视图,请再次输入字段名称,例如:

rails generate nifty:scaffold portfolio name:string
rails generate nifty:scaffold Admin::portfolio  name:string --skip-model

我通常将两者一起执行,因此只需转到上一个命令并编辑它以添加 Admin:: & 就很容易了。 --跳过模型。

Updated as per @tybro0103

Use nifty:generators: https://github.com/ryanb/nifty-generators

rails generate nifty:scaffold Admin::Portfolio

If you have already generated the model or scaffold without namespace and would like to do it again for admin namespace, you can skip model:

rails generate nifty:scaffold Admin::Portfolio --skip-model

If you would like the scaffold to generate views with all fields, please put the field names again, e.g:

rails generate nifty:scaffold portfolio name:string
rails generate nifty:scaffold Admin::portfolio  name:string --skip-model

I usually do the two together so its easy to just go to previous command and edit it to add Admin:: & --skip-model.

残花月 2024-10-01 22:30:08

您可以相当简单地创建自己的生成器并用它们做任何您想做的事情:

在 Rails 4 中:

#config/application.rb
config.generators do |g|
  g.scaffold_controller :my_controller
end

并且

#lib/generators/rails/my_controller/my_controller_generator.rb
class Rails::MyControllerGenerator < Rails::Generators::ScaffoldControllerGenerator
  def class_name
   ([file_name]).map!{ |m| m.camelize }.join('::')
  end

  def table_name
    @table_name ||= begin
      base = pluralize_table_names? ? plural_name : singular_name
      ([base]).join('_')
    end
  end
end

将删除模型命名空间。

请记住,如果您自己生成scaffold_controller,则需要显式调用自定义生成器:rails g my_controller 'account/users'

不幸的是,这仅处理控制器。我仍在寻找查看解决方案。

You can fairly simply create your own generators and do whatever you want with them:

In Rails 4:

#config/application.rb
config.generators do |g|
  g.scaffold_controller :my_controller
end

and

#lib/generators/rails/my_controller/my_controller_generator.rb
class Rails::MyControllerGenerator < Rails::Generators::ScaffoldControllerGenerator
  def class_name
   ([file_name]).map!{ |m| m.camelize }.join('::')
  end

  def table_name
    @table_name ||= begin
      base = pluralize_table_names? ? plural_name : singular_name
      ([base]).join('_')
    end
  end
end

Will remove the model namespacing.

Bear in mind if you are generating a scaffold_controller on its own you'll need to explicitly call your custom generator: rails g my_controller 'account/users'

Unfortunately this only handles the controller. I'm still searching for a view solution.

旧情勿念 2024-10-01 22:30:08

您现在可以使用以下命令在 Rails 上(或至少在 5.1 上)执行此操作:

rails g scaffold_controller admin/portfolio --model-name=Portfolio

通过指定 --model-name Rails 不会自动尝试猜测模型名称。

You can do this now on Rails (or at least on 5.1) using the following command:

rails g scaffold_controller admin/portfolio --model-name=Portfolio

By specifying --model-name Rails does not automatically tries to guess the model name.

咽泪装欢 2024-10-01 22:30:08

不久前,Nifty 生成器是正确的事情,但现在随着 Rails 4 的发布,它已经过时了。如果您想使用非命名空间模型生成管理脚手架,可以使用 rails-admin-scaffold 宝石。

Nifty generators was the right thing some time ago, but now as Rails 4 released it became outdated. If you want to generate admin scaffolding with non-namespaced model you can use rails-admin-scaffold gem.

沉默的熊 2024-10-01 22:30:08

最好的解决方案

rails generate scaffold admin/theme name:string active:integer position:integer --migration=false

rails generate migration CreateTheme name:string active:integer position:integer
# add t.timestamps in migration

所以它不会在表中生成前缀

The best solution

rails generate scaffold admin/theme name:string active:integer position:integer --migration=false

rails generate migration CreateTheme name:string active:integer position:integer
# add t.timestamps in migration

So it does not generate a prefix in the tables

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文