Rails 入门教程 5.7:未正确创建文章对象 [英] Getting Started with Rails Tutorial 5.7: Article object is not being created correctly
问题描述
导轨:4.1.2红宝石:2.1.2
Rails: 4.1.2 Ruby: 2.1.2
我正在尝试完成入门教程 (http://guides.rubyonrails.org/getting_started.html),但在第 5.7 节中遇到问题.当我的标题和文本都应该正确显示时,我得到一个未知属性:标题".我怀疑是文章对象没有被正确创建或保存,但我一直没能找到错误.
I'm trying to complete the Getting Started tutorial (http://guides.rubyonrails.org/getting_started.html) but am having trouble with section 5.7. At the point when my Title and Text should both be displaying correctly, I am getting an "unknown attribute: title". My suspicion is that the article object is not being created or saved correctly, but I haven't been able to find the bug.
这是我的articles_controller.rb:
Here is my articles_controller.rb:
class ArticlesController < ApplicationController
def new
end
def create
# render plain: params[:article].inspect
@article = Article.new(article_params)
@article.save
redirect_to @article
end
def show
@article = Article.find(params[:id])
end
def article_params
params.require(:article).permit(:title, :text)
end
private :article_params
end
我的 show.html.erb:
My show.html.erb:
<p>
<strong>Title:</strong>
<%= @article.title %>
</p>
<p>
<strong>Text:</strong>
<%= @article.text %>
</p>
还有我的佣金路线:
Prefix Verb URI Pattern Controller#Action
articles GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
new_article GET /articles/new(.:format) articles#new
edit_article GET /articles/:id/edit(.:format) articles#edit
article GET /articles/:id(.:format) articles#show
PATCH /articles/:id(.:format) articles#update
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#destroy
welcome_index GET /welcome/index(.:format) welcome#index
root GET / welcome#index
还有一些可能令人感兴趣的是错误页面报告的参数,它们看起来不像我预期的那样,但我知道什么?
Also something that may be of interest are the parameters as reported by the error page, they didn't look as I would have expected, but what do I know?
参数:
{"utf8"=>"✓",
"authenticity_token"=>"OFbPxhf...2V+e9zu3A=",
"article"=>{"title"=>"sdf",
"text"=>"sadfdf"},
"commit"=>"Save Article"}
有人可以对此有所了解吗?谢谢!!
Can anyone shed any light on this? Thanks!!
推荐答案
基于此,从您的评论中,#
问题是当你创建你的文章模型时,你没有添加标题和文本列.试试这个:
Based on this, from your comment, #<Article id: nil, created_at: nil, updated_at: nil>"
the problem is that when you created your Article model, you didn't add the title and text columns. Try this:
rails g migration add_title_to_articles title:string text:text
rake db:migrate
这应该为您的文章模型添加一个标题属性,您应该很高兴.
That should add a title attribute to your articles model and you should be good to go.
阅读指南的第 5.4 节后,我怀疑您忘记了 generate model 命令的一小部分,并且遗漏了这些字段.而不是这样:
After reading section 5.4 of the guide, I suspect that you forgot a little part of the generate model command and are missing those fields. Instead of this:
rails generate model Article title:string text:text
您可能已经这样做了:
rails generate model Article
这将只生成一个:id、:created_at 和 :updated_at
This would have generated just an :id, :created_at, and :updated_at
http://guides.rubyonrails.org/getting_started.html#创建文章模型
这篇关于Rails 入门教程 5.7:未正确创建文章对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!