簡體   English   中英

如何在 Rails 中為現有的 model 生成表單?

[英]How do you generate the form for an existing model in Rails?

現有model 生成單個文件 (_form.html.erb) 的命令是什么?

在 Rails 中工作 3。

謝謝。

這可能聽起來很傻,但聽我說……當我想開始清潔時,我自己做過幾次這樣的事情。 以下是一個腳本,它將讀取您的架構並生成必要的生成命令來重現它:

require 'rubygems'
require 'active_support/core_ext'
schema = File.read('db/schema.rb')
schema.scan(/create_table "(\w+)",.*?\n(.*?)\n  end/m).each do |name, ddl|
  puts "rails generate scaffold #{name.classify} " +
    ddl.scan(/t\.(\w+)\s+"(\w+)"/).
    reject {|type,name| %w(created_at updated_at).include? name}.
    map {|type,name| "#{name}:#{type}"}.join(' ')
end

如果你運行它,你會得到一系列命令。 在一個新目錄中,創建一個新的 rails 應用程序,然后運行這些命令(只需復制和粘貼它們即可)。 獲取您想要的文件。 完成后刪除目錄。

您可以使用這個“反向腳手架”腳本:

https://github.com/ahe/reverse_scaffold

它將在您的 app/views 文件夾中生成所需的 html.erbb 文件。

它在自述文件頁面上得到了很好的解釋。

而且,它已針對 Rails 3.2 進行了更新

我不相信有生成單個文件的命令……只有整個腳手架。

您是否有理由要生成該文件? 您不確定內容應該是什么?

if you don't already have the controller for your model you can generate the controller, specify your entry points and it will create the views for you, however, if you want to have the _form.html.* file already written with the "丑陋的”默認視圖,您可能必須使用腳手架來做到這一點。

這是關於生成器和其他 rails 命令行選項的好指南

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM