I am trying to create a new action list in my previously created Blog using scaffold. I am getting an error in
Couldn't find Blog with 'id'=
on controllers/blogs_controller.rb
Here is my controller code.
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
end
and my routes.rb is
Rails.application.routes.draw do
resources :blogs do
collection do
get :list, :as => 'list'
end
end
root to: 'home#index'
end
Now, in my new controller view called list.html.erb, I need to see all the blog post apart from the normal show action.
In my list.html.erb
<td><%= link_to blog.title %></td>
The error is showing me in set_blog
private
# Use callbacks to share common setup or constraints between actions.
def set_blog
@blog = Blog.find(params[:id])
end
By including the :list
method in your set_blog
before action you are forcing it to look for specific blog using the id as a param, but what you want is actually what you have in the list method, which is pulling in all blogs.
So this
def set_blog
@blog = Blog.find(params[:id])
end
is overriding this
def list
@blogs = Blog.all
end
To fix change this
before_action :set_blog, only: [:show, :edit, :update, :destroy, :list]
to this by removing the :list
method from the set blog before action
before_action :set_blog, only: [:show, :edit, :update, :destroy]
Remove
:list
from before_action
:set_blog
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.