0
我收到此錯誤,每當我刪除某個博客文章。它聽起來合乎邏輯,因爲它正在搜索@blog = Blog.find(params[:id])
,並且由於刪除的人丟失了它的ID,它很可能會拋出該錯誤。但在我的博客控制器中,我確實編寫了一個重定向選項,以便在帖子被刪除後用戶返回主博客路徑,但它不起作用。ActiveRecord :: RecordNotFound - 無法找到博客'id'
銷燬方法:
def destroy
if @blog.destroy
redirect_to blog_path
end
end
我也嘗試添加一個if語句來檢查帕拉姆是否在數據庫或者做一個重定向來代替:
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)
什麼的輸出'Blog.find(4)'在'軌console'? – sureshprasanna70
@ sureshprasanna70'ActiveRecord :: RecordNotFound:無法找到'id'= 4'的博客 – Raymond
根據代碼,你正在銷燬整個博客(而不是博客文章),然後你重定向到它。 – BroiSatse