有没有办法在 Rails 中搭建单例资源? [英] Is there a way to scaffold a singleton resource in rails?

查看:41
本文介绍了有没有办法在 Rails 中搭建单例资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The regular way of scaffolding doesn't work if you are working with a singular resource, is there any way to get default singular controllers through scaffolding? Say if a user has only one post is there an easy was to run a command like:

rails g scaffold post -singular

推荐答案

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

When you look at the scaffold options in Rails 3.1.1, you will see the following:

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

所以看起来脚手架生成了通常的视图模板.

So it seems that the scaffolding generates the usual view templates.

在 Rails 3.0 中,这是整个生成器的一个选项,而不仅仅是 Rspec 的选项.请参阅 Railscast 216 用于 Rails 3 中的生成器.也许您会在 Rails 3.0.x 中找到一个可以满足您需求的生成器.

In Rails 3.0 this was an option for the whole generator, not only the one for Rspec. See the Railscast 216 for Generators in Rails 3. Perhaps you will find a generator in Rails 3.0.x that will fulfill your needs.

这篇关于有没有办法在 Rails 中搭建单例资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆