简体   繁体   English

创建新动作以在Rails中脚手架创建的控制器时出错

[英]Error when creating new action to scaffold created controller in rails

I am trying to create a new action list in my previously created Blog using scaffold. 我正在尝试使用脚手架在先前创建的Blog中创建新的动作列表。 I am getting an error in 我在输入错误

Couldn't find Blog with 'id'= 找不到带有'id'=的博客

on controllers/blogs_controller.rb 在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 而我的routes.rb是

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. 现在,在新的名为list.html.erb的控制器视图中,除了正常的show操作之外,我还需要查看所有博客文章。

In my list.html.erb 在我的list.html.erb中

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

The error is showing me in set_blog 错误在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. 通过在动作之前在set_blog包括:list方法,您可以强制其使用id作为参数来查找特定的博客,但实际上,您想要的实际上是list方法中拥有的东西,该方法将拉入所有博客中。

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 为此,请在操作前从设置的博客中删除:list方法

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

Remove 去掉

:list :列表

from before_action 来自before_action

:set_blog :set_blog

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM