有没有办法在rails中构建单例资源?

时间:2011-11-03 06:10:38

标签: ruby-on-rails ruby-on-rails-3 resources

如果您使用单一资源,脚手架的常规方法不起作用,有没有办法通过脚手架获得默认的奇异控制器?假设一个用户只有一个帖子就可以轻松运行如下命令:

rails g scaffold post -singular

2 个答案:

答案 0 :(得分:2)

当您查看Rails 3.1.1中的脚手架选项时,您将看到以下内容:

Usage:
  rails generate scaffold NAME [field:type field:type] [options]

Options:
  ...

ActiveRecord options:
  ...

Rspec options:
  [--singleton]                 # Supply to create a singleton controller

rails g scaffold Post name:string body:text --singleton的输出是

  invoke  active_record
  create    db/migrate/20111103072825_create_posts.rb
  create    app/models/post.rb
  invoke    rspec
  create      spec/models/post_spec.rb
   route  resources :posts
  invoke  scaffold_controller
  create    app/controllers/posts_controller.rb
  invoke    haml
  create      app/views/posts
  create      app/views/posts/index.html.haml
  create      app/views/posts/edit.html.haml
  create      app/views/posts/show.html.haml
  create      app/views/posts/new.html.haml
  create      app/views/posts/_form.html.haml
  invoke    rspec
  create      spec/controllers/posts_controller_spec.rb
  create      spec/views/posts/edit.html.haml_spec.rb
  create      spec/views/posts/new.html.haml_spec.rb
  create      spec/views/posts/show.html.haml_spec.rb
  invoke      helper
  create        spec/helpers/posts_helper_spec.rb
  create      spec/routing/posts_routing_spec.rb
  invoke      rspec
  create        spec/requests/posts_spec.rb
  invoke    helper
  create      app/helpers/posts_helper.rb
  invoke      rspec
  invoke  assets
  invoke    coffee
  create      app/assets/javascripts/posts.js.coffee
  invoke    scss
  create      app/assets/stylesheets/posts.css.scss
  invoke  scss
identical    app/assets/stylesheets/scaffolds.css.scss

所以脚手架似乎会生成通常的视图模板。

在Rails 3.0中,这是整个发电机的一个选项,而不仅仅是Rspec的发电机。请参阅Railscast 216 for Generators in Rails 3。也许您会在Rails 3.0.x中找到满足您需求的生成器。

答案 1 :(得分:0)

我同意这一点,所以我创建了一个Github issue。如果它有帮助,请遵循此但是我发现修复此问题的唯一方法是手动修复问题。这意味着手动更改名称。我知道你不想这样做,但我发现有些时候这是唯一的方法。