在 ruby​​ on Rails 中手动搭建和更新属性如何?

发布于 2024-09-10 16:11:40 字数 341 浏览 0 评论 0原文

我想知道是否有人知道如何更新使用 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 技术交流群。

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

发布评论

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

评论(4

陈独秀 2024-09-17 16:11:40

本例中的完整命令:

$ rails generate migration add_studentid_to_student

Full command in this example:

$ rails generate migration add_studentid_to_student
女中豪杰 2024-09-17 16:11:40

您需要新属性的新迁移文件,从控制台:

$ script/gnerate migration add_sudentid_to_sudent

它将生成 your_app/db/migrate/8293898391_add_sudentid_to_sudent.rb,在此文件中指定您的新属性:

def self.up
  add_column :sudents, :studentId, :integer
end

def self.down
 remove_column :students, :studentsId
end  

之后,返回控制台:

$  rake db:migrate

然后您可以编辑视图、模型、控制器文件并使用新属性

You need new migration file for new attributes, from console:

$ script/gnerate migration add_sudentid_to_sudent

it will generate your_app/db/migrate/8293898391_add_sudentid_to_sudent.rb, spicify in this file your new attributes:

def self.up
  add_column :sudents, :studentId, :integer
end

def self.down
 remove_column :students, :studentsId
end  

after that, back to console:

$  rake db:migrate

and than you can edit your views, model, controller files and use new attribute

玩世 2024-09-17 16:11:40

您好,尝试ruby script/destroyscaffold Student,然后ruby script/generatescaffold Student

Hi Try ruby script/destroy scaffold student and then ruby script/generate scaffold student

伊面 2024-09-17 16:11:40

还可以尝试阅读 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

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