ruby-on-rails – 有没有办法在rails中搭建单例资源?

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 有没有办法在rails中搭建单例资源?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如果你正在使用一个单一的资源,脚手架的常规方法不起作用,有没有办法通过脚手架获得默认的奇异控制器?假如一个用户只有一个帖子,那么就可以运行一个简单的命令:
rails g scaffold post -singular

解决方法

当您查看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的输出帖子名称:string body:text –singleton is

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中找到满足您需求的生成器.

猜你在找的Ruby相关文章