在 ruby on Rails 中手动搭建和更新属性如何?
我想知道是否有人知道如何更新使用 ruby on Rails 中的脚手架生成器生成的文件(添加/删除/更新属性)。
例如:
脚手架学生姓名:字符串姓氏:字符串
因此这将创建一个关联文件(控制器,视图等),其中名称和姓氏作为字符串属性。当您 db:migrate 项目时,它将在数据库中创建表。但是,假设我想更新,无论是使用附加属性(例如studenId:整数)进行更新,还是删除或更新属性,您该怎么做?
我只是厌倦了更新生成的文件,但是当我执行 db:migrate 时,它仍然将生成的模式设置为表中的内容。 Rails 中是否有内置脚本可以更新表格?
有什么建议吗? 谢谢, D
I was wondering if anyone knew how to update the files (adding/removing/updating an attribute) produced by using the scaffold generator in ruby on rails.
For example:
scaffold student name:string lastname:string
so this will create a the associate files (controller,view,etc) with name and lastname as string attributes. When you db:migrate the project, it'll create the table in the database. However, say I want to update whether it be update it with an addition attribue (ex. studenId:integer) or if its removing or updating an attribute, how do you do that?
I tired just updating the generated files, but when I do that db:migrate it still sets the schema that is generated to what is in the table. Is there a built in script in rails that will update the table?
Any advise appreciated?
Thanks,
D
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
本例中的完整命令:
Full command in this example:
您需要新属性的新迁移文件,从控制台:
它将生成 your_app/db/migrate/8293898391_add_sudentid_to_sudent.rb,在此文件中指定您的新属性:
之后,返回控制台:
然后您可以编辑视图、模型、控制器文件并使用新属性
You need new migration file for new attributes, from console:
it will generate your_app/db/migrate/8293898391_add_sudentid_to_sudent.rb, spicify in this file your new attributes:
after that, back to console:
and than you can edit your views, model, controller files and use new attribute
您好,尝试
ruby script/destroyscaffold Student
,然后ruby script/generatescaffold Student
Hi Try
ruby script/destroy scaffold student
and thenruby script/generate scaffold student
还可以尝试阅读 Rails 迁移,以删除/更新表列。
http://api.rubyonrails.org/classes/ActiveRecord/Migration.html
also try reading up on rails migrations, for dropping/updating table columns.
http://api.rubyonrails.org/classes/ActiveRecord/Migration.html