ActiveRecord :: RecordNotFound - 无法找到带有' id'

时间:2016-02-29 13:17:09

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

每当我删除某篇博文时,我都会收到此错误消息。它确实合乎逻辑,因为它正在搜索@blog = Blog.find(params[:id]),并且由于已删除的一个丢失了它的ID,它很可能会抛出该错误。但是在我的博客控制器中,我确实编写了一个重定向选项,这样当帖子被删除时,用户会回到主博客路径,但它无效。

破坏方法:

  def destroy
    if @blog.destroy
      redirect_to blog_path
    end
  end

我还尝试添加if语句来检查param是否在数据库中,或者改为重定向:

  def show
    @user = User.find_by_id(params[:id])
    @questions = Question.all.order("created_at desc")
    @question = Question.find_by_id(params[:id])
    @blogs = Blog.all.order("created_at desc")
    if @blog 

    else
      redirect_to blog_path
    end
  end 

但似乎都没有效果。这是博客控制器:

class BlogsController < ApplicationController
  before_action :set_blog, only: [:show, :edit, :update, :destroy]

  respond_to :html

  def index
    @blogs = Blog.all.order("created_at desc").paginate(:page => params[:page], :per_page => 15)
    @questions = Question.all.order("created_at desc")
  end

  def show
    @user = User.find_by_id(params[:id])
    @questions = Question.all.order("created_at desc")
    @question = Question.find_by_id(params[:id])
    @blogs = Blog.all.order("created_at desc")
    if @blog 

    else
      redirect_to blog_path
    end
  end

  def new
    @questions = Question.all.order("created_at desc")
    @blogs = Blog.all.order("created_at desc")
    if user_signed_in?
      @blog = current_user.blogs.build
    else
      redirect_to new_user_session_path
    end
  end

  def edit
    @questions = Question.all.order("created_at desc")
    @blogs = Blog.all.order("created_at desc")
  end

  def create
    @blog = current_user.blogs.build(blog_params)
    @blog.save
    redirect_to :back
  end

  def update
    @blog.update(blog_params)
    respond_with(@blog)
  end

  def destroy
    if @blog.destroy
      redirect_to blog_path
    end
  end

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

    def blog_params
      params.require(:blog).permit(:title, :content, :avatar)
    end
end

节目视图:

<div class="signin">
    <div class="container">
    <h3 class="media-heading red noHoverRed"><%= @blog.title %></h3>
    <hr/>
        <div class="row">
            <div class="col-md-8">
              <div class="well">
                  <div class="media">
                    <div class="media-body">
                      <br/>
                      <p><%= @blog.content.html_safe %></p>
                   </div>
                   <div class="pull-right">
                    <% if user_signed_in? %>
                        <% if @blog.user.username == current_user.username %>
                            <%= link_to 'edit', edit_blog_path(@blog), :class => "text-muted links" %>
                            <%= link_to "", blog_path(@blog), :class => "fa fa-close text-muted links", :method => :delete %>
                        <% end %>
                    <% end %>           
                   </div>
                </div>
              </div>

              <!-- Auther -->

              <div class="well">
                  <div class="media">
                    <a class="pull-left">
                    <% if @blog.user.avatar.blank? %>
                        <img src="http://www.adtechnology.co.uk/images/UGM-default-user.png" style="width: 75px;">
                    <% elsif @blog.user.avatar %>
                        <%= image_tag @blog.user.avatar, :style => "width:75px;" %>
                    <% end %>
                    </a>
                    <div class="media-body">
                      <p>About <%= link_to @blog.user.username, @blog.user, :class => " bg" %></p>
                   </div>
                    <% if @blog.user.about.blank? %>
                        <p class="text-left">Apparently, this user prefers to keep their information a mystery.</p>
                    <% else %>
                        <p><%= @blog.user.about.html_safe %></p>
                    <% end %>
                </div>
              </div>

        <!-- Comments -->

              <div class="well">
                <%= render :partial => @blog.comments %>
              </div>
              <br/>

              <div class="well">
                <%= form_for [@blog, Comment.new], :class => 'form-horizontal' do |f| %>
                    <%= f.text_area :body %>
                    <%= f.submit %>
                <% end %>
              </div>
            </div>


            <div class="col-md-4">
                <div class="well">
                <br>
                    <p><span style="padding-right: 10px;" ><i class="fa fa-calendar"></i> posted <%= time_ago_in_words(@blog.created_at) %> ago </span></p>
                </div>
                <br/>
                <div class="well">
                    <h4 class="red">Recent Questions</h4>
                    <hr />
                    <% @questions[0,7].each do |question| %>
                        <p><%= link_to question.title, question %></p>
                    <% end %>
                </div>
                <br/>
                <div class="well">
                    <h4 class="red">Sponsors</h4>
                    <hr />
                    <img src="http://webneel.com/daily/sites/default/files/images/daily/03-2013/4-animal-rights-sweden-boxer-animal-ad.jpg" class="img-responsive">
                </div>
            </div>
        </div>
    </div>
</div>

博客模型:

class Blog < ActiveRecord::Base
    mount_uploader :avatar, AvatarUploader
    belongs_to :user
    has_many :comments, :dependent => :destroy
end

routes.rb中:

Rails.application.routes.draw do  

  resources :questions do
    resources :answers
  end

  resources :blogs do
    resources :comments
  end

  resources :comments

  resources :answers

  devise_for :users
  root 'home#index'
  get '/users/:id' => 'home#profile'
  resources :users

end

我的服务器日志:

ActiveRecord::RecordNotFound (Couldn't find Blog with 'id'=4):
  app/controllers/blogs_controller.rb:57:in `set_blog'


  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.1.8
/lib/action_dispatch/middleware/templates/rescues/_source.erb (1.0ms)
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.1.8
/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.0ms)
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.1.8
/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
 (1.0ms)
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.1.8
/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues
/layout (50.0ms)

1 个答案:

答案 0 :(得分:0)

此修复程序包含sureshprasanna70的评论和更改redirect_to。当我添加了sureshprasanna70建议的if语句时,它停止显示错误,但chrome开始崩溃,因为循环正在重定向到show方法本身。

修复:

自:

  def show
    @user = User.find_by_id(params[:id])
    @questions = Question.all.order("created_at desc")
    @question = Question.find_by_id(params[:id])
    @blogs = Blog.all.order("created_at desc")
    if @blog 

    else
      redirect_to blog_path
    end
  end

要:

  def show
    @user = User.find_by_id(params[:id])
    @questions = Question.all.order("created_at desc")
    @question = Question.find_by_id(params[:id])
    @blogs = Blog.all.order("created_at desc")
    if @blog.nil?
      redirect_to blogs_path
    end
  end