【问题标题】:Error when creating new action to scaffold created controller in rails在 Rails 中创建新操作以脚手架创建的控制器时出错
【发布时间】:2017-07-26 14:46:14
【问题描述】:

我正在尝试使用脚手架在我之前创建的博客中创建一个新的操作列表。我在

中遇到错误

找不到带有 'id'= 的博客

关于控制器/blogs_controller.rb

这是我的控制器代码。

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 是

Rails.application.routes.draw do

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

现在,在名为 list.html.erb 的新控制器视图中,我需要查看除正常显示操作之外的所有博客文章。

在我的 list.html.erb 中

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

错误显示在 set_blog 中

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

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 ruby-on-rails-3.2 ruby-on-rails-5


    【解决方案1】:

    通过在您的set_blog 中包含:list 方法,您可以强制它使用id 作为参数来查找特定的博客,但是您想要的实际上是您在list 方法中拥有的,它正在拉在所有博客中。

    所以这个

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

    覆盖这个

    def list
      @blogs = Blog.all
    end
    

    要解决这个问题

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

    通过在操作之前从设置的博客中删除 :list 方法来实现这一点

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

    【讨论】:

    • @raja 它应该是公认的答案,提供更多信息。作为旁注,您的 list 操作复制了您的 index 操作。除非你有充分的理由这样做,否则重写 Rails 鼓励的 REST 约定通常不是一个好主意,而且重复也不是一件好事。但是,如果您真的需要 /blogs/list 路由,您可以使用 get :list, as: :list, to: 'blogs#index' 将其发送到您的 index 操作
    【解决方案2】:

    删除

    :列表

    来自 before_action

    :set_blog

    【讨论】:

    • 这个答案和 Rockwell Rice 的答案有什么区别?
    • @raja well Rockwell's 是描述性的。那应该是公认的。 (y)
    猜你喜欢
    • 2012-11-05
    • 2012-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-21
    相关资源
    最近更新 更多