如何在Rails中为现有模型生成表单? [英] How do you generate the form for an existing model in Rails?

查看:65
本文介绍了如何在Rails中为现有模型生成表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现有 模型生成单个文件(_form.html.erb)的命令是什么?

What is the command to generate a single file (_form.html.erb) for an existing model?

在Rails 3中工作.

Working in Rails 3.

谢谢.

推荐答案

这听起来可能很愚蠢,但是请听我说...当我想开始清洁时,我自己做了几次这样的事情.以下是一个脚本,它将读取您的架构并生成必要的生成命令以重现它:

This may sound silly, but hear me out... I've done things like this a few times myself when I wanted to start clean. Following is a script that will read your schema and produce the necessary generate commands to reproduce it:

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应用程序,然后运行这些命令(只需复制并粘贴它们即可).抓取您想要的文件.完成后删除目录.

If you run this, you will get a series of commands. In a new directory, create a new rails application, and then run these commands (simply copying and pasting them will do). Grab the files you want. Delete the directory when you are done.

这篇关于如何在Rails中为现有模型生成表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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