Ruby on rails Rails:充当标记,在尝试按标记查找文章时出错
我一直在关注RailsCasts的一个插曲,并试图使文章具有可标记性。然后我想让标签在articles show.html页面上可以点击,这样用户就可以找到其他相关的文章。我使用的是Rails 4。但是,我得到了以下错误:Ruby on rails Rails:充当标记,在尝试按标记查找文章时出错,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我一直在关注RailsCasts的一个插曲,并试图使文章具有可标记性。然后我想让标签在articles show.html页面上可以点击,这样用户就可以找到其他相关的文章。我使用的是Rails 4。但是,我得到了以下错误: ActiveRecord::RecordNotFound in ArticlesController#show Couldn't find Article without an ID Extracted source (around line #43): def sho
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\u controller.rb
[...]
def show
if params[:tag]
@articles = Article.tagged_with(params[:tag])
else
@article = Article.find(params[:id])
end
end
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
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
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
在工作显示页面上单击标记时出现错误:
NoMethodError in Articles#show
undefined method 'title' for nil:NilClass
<div id="article-title">
<h1>
<p>
<%= @article.title %></span>
</p>
</h1>
</div>
article.rb
[...]
def show
if params[:tag]
@articles = Article.tagged_with(params[:tag])
else
@article = Article.find(params[:id])
end
end
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
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
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
更新
ArticlesController
actionsshow
和index
,如下所示:
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,我仍然得到nil:NilClass的
未定义的方法'title'。应用程序没有以某种方式说明它对修复的想法。任何其他建议都非常感谢!你能分享问题中的stacktrace吗?我更新了帖子。我的选择是完整的、框架的和应用的。如果还需要,请告诉我。谢谢分享。你觉得表演怎么样?分享相关代码或查看我的更新答案,例如,我将如何通过我的应用程序调用它。你是说,当你说call时,my show.html中的
?我不知道你指的是什么文件,除了控制器。我将同时更新def show。