文章中没有方法错误#show
NoMethodError in Articles#show
我正在使用 Rails http://tutorials.jumpstartlab.com/projects/blogger.html
入门教程
我已经完成了教程,现在我正在使用我所做的为学校作业创建网站,我只想在 [=26= 上添加 "data" 文件].rb,但我收到此消息错误
Showing /home/ubuntu/workspace/app/views/articles/show.html.erb where line #12 raised:
undefined method `data' for #<Article:0x007f8bcab2cfe8>
Extracted source (around line #12):
<p>
<strong>Data:</strong>
<%= @article.data %>
</p>
我的articles_controller是这样的
class ArticlesController < ApplicationController
http_basic_authenticate_with name: "mateus", password: "mateus", except: [:index, :show]
def index
@articles = Article.all
end
def show
@article = Article.find(params[:id])
end
def new
@article = Article.new
end
def edit
@article = Article.find(params[:id])
end
def create
@article = Article.new(article_params)
if @article.save
redirect_to @article
else
render 'new'
end
end
def update
@article = Article.find(params[:id])
if @article.update(article_params)
redirect_to @article
else
render 'edit'
end
end
def destroy
@article = Article.find(params[:id])
@article.destroy
redirect_to articles_path
end
def article_params
params.require(:article).permit(:title, :text, :data)
end
end
有人可以帮我吗?
Edit1:我使用了 rake db:migrate:status
然后我得到了这个:
mateusjs:~/workspace (master) $ rake db:migrate:status
database: app_development
Status Migration ID Migration Name
--------------------------------------------------
up 20161005160810 Create articles
up 20161005185521 Create comments
up 20170209222858 Parte2
schema.rb
是否表明您的 Article
具有 data
属性?
一个快速测试的方法是进入你的控制台并
$ bundle exec rails console
> Article
看看 data
是否存在。
如果没有,您可能需要做两件事;
- 进行迁移,即转到终端并执行类似的操作;
$ bundle exec rails g migration AddDataToArticle data:integer
- 完成该步骤后,您需要
bundle exec rails db:migrate
您是否创建了一个新的迁移:
rails g migration AddDataToArticle data:binary
rails db:migrate
data:binary
因为我认为应该存储 "file" 本身。
或者您是否更改了已经定义的迁移。由于目前不存在 data
属性 - ruby 假设这应该是一个方法。
您能否将您的迁移附加到您的问题中?
根据问题的描述和您想要完成的操作,您忘记将迁移应用到您的数据库。
如果您更改了现有的迁移,您需要通过运行以下命令re-apply它(假设您是运行 Rails 5.xx):
rails db:migrate:up VERSION=20161005160810
其他 Rails 版本应接受此命令:
rake db:migrate:up VERSION=20161005160810
如果您创建了新的迁移,您仍然需要应用迁移。以下是您在 Rails 5.xx:
中的操作
rails db:migrate
其他 Rails 版本应接受此命令:
rake db:migrate
希望对您有所帮助!
我正在使用 Rails http://tutorials.jumpstartlab.com/projects/blogger.html
入门教程我已经完成了教程,现在我正在使用我所做的为学校作业创建网站,我只想在 [=26= 上添加 "data" 文件].rb,但我收到此消息错误
Showing /home/ubuntu/workspace/app/views/articles/show.html.erb where line #12 raised:
undefined method `data' for #<Article:0x007f8bcab2cfe8>
Extracted source (around line #12):
<p>
<strong>Data:</strong>
<%= @article.data %>
</p>
我的articles_controller是这样的
class ArticlesController < ApplicationController
http_basic_authenticate_with name: "mateus", password: "mateus", except: [:index, :show]
def index
@articles = Article.all
end
def show
@article = Article.find(params[:id])
end
def new
@article = Article.new
end
def edit
@article = Article.find(params[:id])
end
def create
@article = Article.new(article_params)
if @article.save
redirect_to @article
else
render 'new'
end
end
def update
@article = Article.find(params[:id])
if @article.update(article_params)
redirect_to @article
else
render 'edit'
end
end
def destroy
@article = Article.find(params[:id])
@article.destroy
redirect_to articles_path
end
def article_params
params.require(:article).permit(:title, :text, :data)
end
end
有人可以帮我吗?
Edit1:我使用了 rake db:migrate:status
然后我得到了这个:
mateusjs:~/workspace (master) $ rake db:migrate:status
database: app_development
Status Migration ID Migration Name
--------------------------------------------------
up 20161005160810 Create articles
up 20161005185521 Create comments
up 20170209222858 Parte2
schema.rb
是否表明您的 Article
具有 data
属性?
一个快速测试的方法是进入你的控制台并
$ bundle exec rails console
> Article
看看 data
是否存在。
如果没有,您可能需要做两件事;
- 进行迁移,即转到终端并执行类似的操作;
$ bundle exec rails g migration AddDataToArticle data:integer
- 完成该步骤后,您需要
bundle exec rails db:migrate
您是否创建了一个新的迁移:
rails g migration AddDataToArticle data:binary
rails db:migrate
data:binary
因为我认为应该存储 "file" 本身。
或者您是否更改了已经定义的迁移。由于目前不存在 data
属性 - ruby 假设这应该是一个方法。
您能否将您的迁移附加到您的问题中?
根据问题的描述和您想要完成的操作,您忘记将迁移应用到您的数据库。
如果您更改了现有的迁移,您需要通过运行以下命令re-apply它(假设您是运行 Rails 5.xx):
rails db:migrate:up VERSION=20161005160810
其他 Rails 版本应接受此命令:
rake db:migrate:up VERSION=20161005160810
如果您创建了新的迁移,您仍然需要应用迁移。以下是您在 Rails 5.xx:
中的操作rails db:migrate
其他 Rails 版本应接受此命令:
rake db:migrate
希望对您有所帮助!