0

scaffoldを使用して以前に作成したブログに新しいアクションリストを作成しようとしています。私は私のコントローラのコードは、ここでレールで作成したコントローラを足場に作成する際にエラーが発生する

にエラーがコントローラー/ blogs_controller.rbに=

'ID' とブログが見つかりませんでし取得しています。

class BlogsController < ApplicationController 
before_action :authenticate_user!, :except => [:index, :show] 
layout 'admin', only:[:new, :create, :update, :destroy, :edit, :list] 
before_action :set_blog, only: [:show, :edit, :update, :destroy, 
:list] 

# GET /blogs 
# GET /blogs.json 
def index 
@blogs = Blog.all 
end 

# GET /blogs/1 
# GET /blogs/1.json 
def show 
end 

def list 
@blogs = Blog.all 
end 

# GET /blogs/new 
def new 
@blog = Blog.new 
end 

# GET /blogs/1/edit 
def edit 
end 

# POST /blogs 
# POST /blogs.json 
def create 
@blog = Blog.new(blog_params) 

respond_to do |format| 
    if @blog.save 
    format.html { redirect_to @blog, notice: 'Blog was successfully   
    created.' } 
    format.json { render :show, status: :created, location: @blog } 
    else 
    format.html { render :new } 
    format.json { render json: @blog.errors, status: :unprocessable_entity } 
    end 
end 
end 

# PATCH/PUT /blogs/1 
# PATCH/PUT /blogs/1.json 
def update 
respond_to do |format| 
    if @blog.update(blog_params) 
    format.html { redirect_to @blog, notice: 'Blog was successfully updated.' } 
    format.json { render :show, status: :ok, location: @blog } 
    else 
    format.html { render :edit } 
    format.json { render json: @blog.errors, status: :unprocessable_entity } 
    end 
end 
end 

# DELETE /blogs/1 
# DELETE /blogs/1.json 
def destroy 
@blog.destroy 
respond_to do |format| 
    format.html { redirect_to blogs_url, notice: 'Blog was successfully destroyed.' } 
    format.json { head :no_content } 
end 
end 

private 
# Use callbacks to share common setup or constraints between actions. 
def set_blog 
    @blog = Blog.find(params[:id]) 
end 

# Never trust parameters from the scary internet, only allow the white list through. 
def blog_params 
    params.require(:blog).permit(:title, :description) 
end 
エンド

と私のroutes.rbを私の新しいコントローラビューと呼ばれるlist.html.erbで、今

Rails.application.routes.draw do 

resources :blogs do 
collection do 
    get :list, :as => 'list' 
end 
end 
root to: 'home#index' 
end 

ですが、私は別に、すべてのブログ投稿を参照してくださいする必要があります通常のショーアクション。私list.html.erbで

<td><%= link_to blog.title %></td> 

エラーは、特定のために見て、それを強制されているアクションの前に、あなたのset_blog:list方法を含めることによりset_blog

private 
# Use callbacks to share common setup or constraints between actions. 
def set_blog 
    @blog = Blog.find(params[:id]) 
end 

答えて

3

で私を示していますあなたが望んでいるのは実際にあなたがリストメソッドで持っているものです。これはすべてのブログを引っ張っています。

だから、この

def set_blog 
    @blog = Blog.find(params[:id]) 
end 

がアクション

前に、設定されたブログから :list方法を除去することにより、これにこの

before_action :set_blog, only: [:show, :edit, :update, :destroy, :list] 

を変更する修正するには、この

def list 
    @blogs = Blog.all 
end 

をオーバーライドしています0

before_action :set_blog, only: [:show, :edit, :update, :destroy] 
+0

作品を魔法のように..あなたに感謝。 – raja

+2

@raja正しいマークを削除したのはなぜですか? –

+1

これは受け入れられる回答である必要があります! – Pavan

0

削除:リスト

before_action

から:set_blog

+0

これとの違いは何ですか?答えとロックウェルライスの答えは? – Pavan

+0

@rajaよくロックウェルズは記述的です。それは受け入れられたものでなければなりません。 (y) –

関連する問題