【问题标题】:Rails: acts_as_taggable_on, getting errors when attempting to find articles by tagRails:acts_as_taggable_on,尝试按标签查找文章时出错
【发布时间】:2014-04-03 23:09:31
【问题描述】:

我一直在关注 RailsCasts 剧集,并试图使文章可标记。然后我想在文章show.html页面上使标签可点击,以便用户可以找到其他相关文章。我正在使用 Rails 4。但是,我收到以下错误:

ActiveRecord::RecordNotFound in ArticlesController#show

Couldn't find Article without an ID

Extracted source (around line #43):

def show 
  @article = Article.find(params[:id])
end

然后我尝试进行以下更改,这是我在查看 RailsCast 代码后观察到的:

articles_controller.rb

[...]
    def show  
        if params[:tag]
          @articles = Article.tagged_with(params[:tag])
        else
          @article = Article.find(params[:id])
        end
    end

点击工作显示页面上的标签时出现错误:

http://localhost:x000/tags/pizza

NoMethodError in Articles#show
undefined method 'title' for nil:NilClass

<div id="article-title">
  <h1>
   <p>
    <%= @article.title %></span>
   </p>
  </h1>
</div>

这对我来说似乎很奇怪,因为 title 方法可以正常工作。网站上的所有其他内容都有效,标签保存并可以在文章编辑页面中编辑,但查找具有特定标签的所有文章不起作用。

=== 相关代码区:

_form.html

<p>
    <%= f.label :keyword_Tags %><br/>
    <%= f.text_field :tag_list %> <span class="gray-text"><em>Separated by commas; single tag has no spaces, only-dashes </em></span>
  </p>

routes.rb

Network::Application.routes.draw do
  resources :libraries

  get "libraries/show"
  devise_for :authors, :controllers => {:registrations => "authors/registrations"}

  get 'tags/:tag', to: 'articles#show', as: :tag 

  devise_scope :author do
    get 'signup', to: 'devise/registrations#new' 
    get 'signin', to: 'devise/sessions#new' 
    # get 'logout', to: 'devise/sessions#destroy' 
  end

  resources :relationships, only: [:create, :destroy]

  get "landing_page/index"
  # The priority is based upon order of creation: first created -> highest priority.
  # See how all your routes lay out with "rake routes".
  resources :articles do
    resources :comments
  end
  # You can have the root of your site routed with "root"
  root 'landing_page#index'
  get '/:id', to: 'libraries#show'
end

article.rb

 class Article < ActiveRecord::Base
    has_many :comments, dependent: :destroy
    belongs_to :author 
    acts_as_taggable
    acts_as_taggable_on :tag_list

    validates :title, presence: true,
                    length: { minimum: 5 }
  validates :text, presence: true,
                                 length: { minimum: 2 }
    validates :article_start, presence: true,
                                        length: {minimum: 10}

end

[num]_create_articles.rb

class CreateArticles < ActiveRecord::Migration
  def change
    create_table :articles do |t|
      t.string :title
      t.time :era
      t.string :chapter
      t.date :article_start
      t.text :text
      t.string :tag_list

      t.timestamps
    end
  end
end

控制器

class ArticlesController < ApplicationController
before_filter :authenticate_author!, only: [:create, :new, :edit, :update, :destroy]
#added destroy to above, not in Treebook

def index
    @articles = Article.all 
end

def new # diff between new and create?
    @article = Article.new
end

def create 
    @article = Article.new(article_params)

    #respond_to do|format|

    if @article.save
        redirect_to @article 
    else
        render 'new'

    end
end
#end #for respond_to; noticed in "What is a join table"

def show #show, being an action
    @article = Article.find(params[:id])

end


def edit
    @article = Article.find(params[:id])
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



private
    def article_params
        params.require(:article).permit(:title, :text, :author_id, :article_start, :era, :chapter, :address, :tag_list)
    end

结束

请求

Parameters:

{"tag"=>"open"}

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4


    【解决方案1】:

    更新ArticlesController动作showindex如下:

       def show  
           @article = Article.find(params[:id])
        end
    
       def index  
         if params[:tag]
           @articles = Article.tagged_with(params[:tag])
         else
           @articles = Article.all
         end
       end
    

    另外,更新routes.rb

    替换

     get 'tags/:tag', to: 'articles#show', as: :tag
    

     get 'tags/:tag', to: 'articles#index', as: :tag ## It should go to index action not show
    

    【讨论】:

    • 嗨,Kirti,我仍然收到undefined method 'title' for nil:NilClass。该应用程序没有以一种或另一种方式说出它对修复的看法。非常感谢任何其他建议!
    • 你能分享问题中的堆栈跟踪吗?
    • 我更新了帖子。我的选择是完整的、框架和应用程序。如果需要,请告诉我。
    • 感谢分享。你怎么称呼表演动作?分享相关代码或查看我的更新答案,例如我将如何通过我的应用程序调用它。
    • 嗨,Kirti,我是一名 Rails 新手。你的意思是,当你说呼叫时,我的 show.html 中的&lt;%= raw @article.tag_list.map { |t| link_to t, tag_path(t) }.join(', ')%&gt;?除了控制器,我不知道您指的是什么文件。我会同时更新def show。
    猜你喜欢
    • 1970-01-01
    • 2023-02-09
    • 1970-01-01
    • 2016-04-29
    • 1970-01-01
    • 1970-01-01
    • 2017-12-11
    • 2021-06-20
    • 2012-08-07
    相关资源
    最近更新 更多