Ruby on Rails 生成视图

发布于 2024-10-31 17:44:59 字数 111 浏览 0 评论 0原文

有没有办法使用railsgenerate命令单独生成视图?我也愿意安装一个 gem 来完成现有的任务。基本上,脚手架命令给了我太多的东西,我宁愿手动编写我的控制器。但是,使用记录表编写索引视图的效率不是很高。

Is there a way to generate the views separately using the rails generate command? I would also be willing to install a gem to accomplish that task f one exists. Basically the scaffolding command gives me too much and I would rather code my controller by hand. However, writing the index view with a table for the records would not be very efficient.

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

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

发布评论

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

评论(5

南薇 2024-11-07 17:44:59

您可以使用控制器生成器生成控制器和视图。

rails g controller controllername new create

这将创建操作 newcreate 及其相应的视图。

您仍然需要手动设置路线。

You can generate the controller and the view using the controller generator.

rails g controller controllername new create

This will create actions new and create with their corresponding views.

You still need to set up your routes manually with this.

奢欲 2024-11-07 17:44:59

一种特殊情况是当您想要向现有控制器添加视图时。

在这种情况下,只需使用常规命令,但每次提示时都要小心说'n',以免覆盖现有文件。

例如,将名为 'invite' 的视图添加到名为 'projects' 的现有控制器:

smith@ubuntuSrv16DEV4:~/railsapps/project_manager$ rails -v
Rails 5.1.4
smith@ubuntuSrv16DEV4:~/railsapps/project_manager$ rails generate controller projects invite
Running via Spring preloader in process 46253
    conflict  app/controllers/projects_controller.rb
Overwrite /home/smith/railsapps/project_manager/app/controllers/projects_controller.rb? (enter "h" for help) [Ynaqdh] n
        skip  app/controllers/projects_controller.rb
       route  get 'projects/invite'
      invoke  erb
       exist    app/views/projects
      create    app/views/projects/invite.html.erb
      invoke  test_unit
    conflict    test/controllers/projects_controller_test.rb
  Overwrite /home/smith/railsapps/project_manager/test/controllers/projects_controller_test.rb? (enter "h" for help) [Ynaqdh] n
        skip    test/controllers/projects_controller_test.rb
      invoke  helper
   identical    app/helpers/projects_helper.rb
      invoke    test_unit
      invoke  assets
      invoke    coffee
   identical      app/assets/javascripts/projects.coffee
      invoke    scss
    conflict      app/assets/stylesheets/projects.scss
    Overwrite /home/smith/railsapps/project_manager/app/assets/stylesheets/projects.scss? (enter "h" for help) [Ynaqdh] n
        skip      app/assets/stylesheets/projects.scss
smith@ubuntuSrv16DEV4:~/railsapps/project_manager$ 

One particular situation is when you want to add a new view to an existing controller.

In that case, just use the regular command, but be careful to say 'n' every time prompted in order to not overwrite existing files.

For example, adding a view called 'invite' to an existing controller named 'projects':

smith@ubuntuSrv16DEV4:~/railsapps/project_manager$ rails -v
Rails 5.1.4
smith@ubuntuSrv16DEV4:~/railsapps/project_manager$ rails generate controller projects invite
Running via Spring preloader in process 46253
    conflict  app/controllers/projects_controller.rb
Overwrite /home/smith/railsapps/project_manager/app/controllers/projects_controller.rb? (enter "h" for help) [Ynaqdh] n
        skip  app/controllers/projects_controller.rb
       route  get 'projects/invite'
      invoke  erb
       exist    app/views/projects
      create    app/views/projects/invite.html.erb
      invoke  test_unit
    conflict    test/controllers/projects_controller_test.rb
  Overwrite /home/smith/railsapps/project_manager/test/controllers/projects_controller_test.rb? (enter "h" for help) [Ynaqdh] n
        skip    test/controllers/projects_controller_test.rb
      invoke  helper
   identical    app/helpers/projects_helper.rb
      invoke    test_unit
      invoke  assets
      invoke    coffee
   identical      app/assets/javascripts/projects.coffee
      invoke    scss
    conflict      app/assets/stylesheets/projects.scss
    Overwrite /home/smith/railsapps/project_manager/app/assets/stylesheets/projects.scss? (enter "h" for help) [Ynaqdh] n
        skip      app/assets/stylesheets/projects.scss
smith@ubuntuSrv16DEV4:~/railsapps/project_manager$ 
送君千里 2024-11-07 17:44:59

正如 Sameers 之前提到的,有一篇文章展示了如何生成视图。它将使用 Rails 默认模板为您的模型创建所有视图,这非常方便。

如果像我一样你想要一些更可定制的东西,你可以实现以下目标。

您可以创建自己的生成器,这样您就拥有了这样的东西。

Rails 生成视图 NAME VIEW [选项]

要实现此目的,您需要执行以下操作。

railsgenerategeneratorview

这将在 lib/generators/view/ 文件夹中为您生成一些文件。

打开 view_generator.rb 文件并添加以下代码。

class ViewGenerator < Rails::Generators::Base
  source_root File.expand_path('templates', __dir__)
  argument :name, type: :string
  argument :action, type: :string

  def generate_view
    template "#{file_name}.html.erb", "app/views/#{folder_name}/#{file_name}.html.erb"
  end

  private

  def folder_name
    name.underscore
  end

  def file_name
    action.underscore
  end

  def type
    name.titleize.singularize
  end

  def down_type
    name.downcase.singularize
  end

  def render_form
    "<%= render 'form', #{down_type}: @#{down_type} %>"
  end

  def render_link_back
    "<%= link_to 'Back', #{folder_name}_path %>"
  end
end</pre>

接下来,您需要创建我们使用在generate_view方法中使用的实际模板的文件。

以操作 new 为例,创建一个 filelib/generators/view/new.html.erb 并添加以下内容。

<h1>New <%= type %></h1>

<%= render_form %>

<%= render_link_back %>

根据需要自定义模板视图。您还需要添加 _form.html.erb。在 view_generator.rb 文件中添加任何其他变量和逻辑即可完成。

这需要更多的工作,但如果您发现自己一直生成类似的视图,那么这可能是值得的。

我能想到的这种方法的最佳用例是,如果您对平台进行白标并且需要为客户配置文件生成多个文件。

As previously mentioned by sameers there was post that showed how to just generate a the views. It will create all the views for your model using the rails default templates which is very handy.

If like me you want something a little more customizable you can achieve the following.

You can create your own generator so you have something like this.

rails generate view NAME VIEW [options]

To achieve this you need to do the following.

rails generate generator view

This will generate a few files for you in lib/generators/view/ folder.

Open the view_generator.rb file and add the following code.

class ViewGenerator < Rails::Generators::Base
  source_root File.expand_path('templates', __dir__)
  argument :name, type: :string
  argument :action, type: :string

  def generate_view
    template "#{file_name}.html.erb", "app/views/#{folder_name}/#{file_name}.html.erb"
  end

  private

  def folder_name
    name.underscore
  end

  def file_name
    action.underscore
  end

  def type
    name.titleize.singularize
  end

  def down_type
    name.downcase.singularize
  end

  def render_form
    "<%= render 'form', #{down_type}: @#{down_type} %>"
  end

  def render_link_back
    "<%= link_to 'Back', #{folder_name}_path %>"
  end
end</pre>

Next you need to create the file that we are using actual template used in generate_view method.

Using the action new as example, create a filelib/generators/view/new.html.erb and add the following.

<h1>New <%= type %></h1>

<%= render_form %>

<%= render_link_back %>

Customize the template view as much as you want. You will need to add the _form.html.erb as well. Add any additional variables and logic in your view_generator.rb file and you are done.

It's more work but can be worth it if you find yourself generating similar views all the time.

Best use case I can think of for this approach is if you white label your platform and need to generate multiple files for a clients profile.

花开浅夏 2024-11-07 17:44:59

第一部分是模型/控制器的名称,第二部分是操作。

the first part is the name of the model/controller, the second part are the actions.

橘味果▽酱 2024-11-07 17:44:59

我创建了一个 https://github.com/coezbek/rails_view_generator ,其目的只是创建脚手架视图,而不需要脚手架控制器或模型。

rails g view Model field:type field:type

I have created a https://github.com/coezbek/rails_view_generator for the purpose of only creating the scaffold views, without the scaffold controller or model.

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