2016-06-21 9 views
2

コメントとタグを付けてRuby-on-Railsブログのウェブサイトを作成する方法についてはtutorialに従っており、これまでのところ私の仕事はhttps://github.com/khpeek/jumpstart-blogger/になっています。UnknownAttributeError on Railsウェブサイト

問題は、私は、エラーメッセージ(下記参照)「ArticlesController#でのActiveRecord :: UnknownAttributeErrorを作成」を取得

enter image description here

、私は以下に示すように、タグ付き新品を作成しようとするということです。

enter image description here

しかし、チュートリアルに従って私は、この時点で記事は「通過」することを期待すべきです。 articles_controller.rbtag_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 

を、I tag_listArticleの属性として認識されない理由を理解できません。どうすればこの問題を解決できますか?

+0

記事にデータベース列として追加する必要があります。マイグレーションを使用してください。コントローラ内の 'def tag_list =(tags_string)'メソッドは何もしていないように見えますが、おそらくArticleモデルにあるはずです。 –

答えて

6

追加article.rb

def tag_list=(tags_string) 
    # first split the tags based on "," which is coming from the form 
    tag_names = tags_string.split(",").collect{|s| s.strip.downcase}.uniq 
    # search if any particular tag is present or not, based on that assign them 
    new_tags = tag_names.collect { |name| Tag.find_or_create_by(name: name) } 
    self.tags = new_tags 
end 

をまた、articlesController.rbアド:

def article_params 
params.require(:article).permit(:title, :body, :tag_list) 
end 

それが役に立てば幸い!

+1

また、質問の 'ArticlesController'で空の' def tag_list =(tags_string) 'を削除することもできます。 'article.rb'の' Article'に対して定義された 'tag_list ='は 'Article.new(article_params)'によって呼び出される唯一のバージョンです。 –

+0

答えをありがとう。これは、 'article.rb'のメソッド定義に' def tag_list =(tags_string) '(' def tag_list'だけではなく)を含めることです。 Rory O'Kaneが提案したように 'ArticlesController'の空のメソッドを削除し、' def article_params'を含める必要はありませんでした(少なくとも、エラーを取り除くためではありません)。 –

+0

修正:Emuによって与えられた 'def article_params'コードが必要ですが、既に' ArticlesHelper'モジュール(これは私がリストしていない)に含まれています。 –

0

あなたはRails 4を使用しているようです。あなたは強力なパラメータ許可リストにtag_listを含めましたか?あなたに