我一直关注着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>
这对我来说似乎很奇怪,因为标题方法在其他情况下工作正常。其他所有内容都在网站上运行,标签保存并可以在文章编辑页面中进行编辑,但查找具有特定标签的所有文章都不起作用。
=== 相关代码区:
_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"}
答案 0 :(得分:1)
更新ArticlesController
行动show
和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