简体   繁体   English

在Ruby on Rails中编辑资源

[英]Editing a resource in Ruby on Rails

To begin with, I must say that I am VERY new to Ruby on Rails. 首先,我必须说我对Ruby on Rails非常新。 I've only been working my way through it for about a week. 我只用了大约一个星期就一直在努力。

I've starting integrating my existing website onto a blog I made which uses two resources - posts and comments. 我已经开始将现有网站整合到我制作的博客中,该博客使用两种资源-帖子和评论。

My problem resides with the posts resource. 我的问题在于posts资源。 I made the posts resource using "generate scaffold", to give me the fields "Title" and "Content", however I would now like to add another field "Image", which takes a local url of an image to add to the blog. 我使用“生成脚手架”创建了帖子资源,给我字段“标题”和“内容”,但我现在想添加另一个字段“图像”,它将图像的本地URL添加到博客。

I've gone through manually and added references to the new "Image" field, adding it as a string with the intention of using the string as a target in an image_tag when it need to be displayed. 我已经手动检查并添加了对新“ Image”字段的引用,将其添加为字符串,目的是在需要显示该字符串时将其用作image_tag中的目标。 However when I visit pages that use the form to add or edit posts, I'm given the following error: 但是,当我访问使用表单添加或编辑帖子的页面时,出现以下错误:

NoMethodError in Posts#new
undefined method `image' for #<Post:0x39a4868>

Is there an automated, catch-all way of adding a new field to a pre-existing resource? 有没有一种自动的,万能的方式将新字段添加到现有资源中?

Any help would be appreciated. 任何帮助,将不胜感激。

You need to generate a migration. 您需要生成迁移。

rails g migration add_image_to_posts image:string

Which will generate a file in db/migrate 这将在db / migrate中生成一个文件

You can then run this migration with: 然后,您可以运行此迁移:

rake db:migrate

And you should now have an image field in your model. 您现在应该在模型中有一个图像字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM