我正在關注如何使用評論和標籤創建Ruby-on-Rails博客網站tutorial,並且已將我的工作放到https://github.com/khpeek/jumpstart-blogger/以上。Rails網站上的UnknownAttributeError
的問題是,當我嘗試創建一個標籤一個新的文章,如下圖所示,
我得到一個錯誤信息「的ActiveRecord :: UnknownAttributeError在ArticlesController#創建」(見下文) 。
然而,根據教程我應該在這一點上希望文章爲「經歷」。該articles_controller.rb
有tag_list
一個「設定」方法:
class ArticlesController < ApplicationController
include ArticlesHelper
def index
@articles = Article.all
end
def show
@article = Article.find(params[:id])
@comment = Comment.new
@comment.article_id = @article_id
end
def new
@article = Article.new
end
def tag_list=(tags_string)
end
def create
# fail
@article = Article.new(article_params)
@article.save
flash.notice = "Article '#{@article.title}' created."
redirect_to article_path(@article)
end
def destroy
@article = Article.find(params[:id])
@article.destroy
flash.notice = "Artice '#{@article.title}' deleted."
redirect_to articles_path
end
def edit
@article = Article.find(params[:id])
end
def update
@article = Article.find(params[:id])
@article.update(article_params)
flash.notice = "Article '#{@article.title}' updated."
redirect_to article_path(@article)
end
end
此外,「to_s」的「變量」類的方法已被修改,返回它的名字:
class Tag < ActiveRecord::Base
has_many :taggings
has_many :articles, through: :taggings
def to_s
name
end
end
總之,我不明白爲什麼tag_list
未被識別爲Article
的屬性。我該如何解決這個問題?
您需要添加它作爲文章的數據庫列 - 使用遷移。您的控制器中的'def tag_list =(tags_string)'方法看起來並不像它正在做什麼,它應該可能在Article模型中。 –