Таким образом, у меня есть страница показа обзоров, которая вложена в маршруты альбомов. На странице просмотра отзывов URL был http://localhost: 3000 / album / 2 / reviews / 19 , я изменил 19 на случайное количество отзывов, которых у меня нет. Это привело меня к ошибке "не удалось найти обзор с 'id' = 25". Я хотел бы перенаправить этого пользователя на созданную мной страницу not_found, но я не уверен, как это сделать.
Вот моя ошибка:
ActiveRecord::RecordNotFound in ReviewsController#show
Couldn't find Review with 'id'=25
Extracted source (around line #59):
57
58
59
60
61
62
def set_review
@review = Review.find(params[:id])
end
def set_current_user
Обзоры контроллера:
class ReviewsController < ApplicationController
before_action :set_review, only: [:show, :edit, :update, :destroy]
before_action :set_current_user, only: [:index, :show, :new, :edit, :destroy]
before_action :find_album, only: [:show, :create, :edit, :update, :destroy]
before_action :must_login, only: [:index, :show, :new, :create, :edit, :update, :destroy]
def index
@albums = Album.with_recent_reviews #scope
end
def show
#@reviews = Review.where("album_id = ?", params[:album_id])
end
def new
if params[:album_id] && @album = Album.find_by(id: params[:user_id])
@review = @album.reviews.build
else
redirect_to albums_path
end
end
def create
@review = current_user.reviews.build(review_params)
#@review.album = @album
if @review.save
redirect_to album_path(@album)
else
#@review.album
render :new
end
end
def edit
end
def update
if @review.update(review_params)
redirect_to album_path(params[:album_id])
else
render 'edit'
end
end
def destroy
if current_user.id == @review.user_id
@album.reviews.find(params[:id]).destroy
redirect_to album_path(params[:album_id])
else
flash[:error] = "Unable to delete your review. Please try again."
redirect_to album_reviews_path(@review)
end
end
private
def set_review
@review = Review.find(params[:id])
end
def set_current_user
@user = current_user
end
def find_album
@album = Album.find(params[:album_id])
end
def review_params
params.require(:review).permit(:title, :date, :content, :album_id, album_attributes:[:artist, :title, :user_id])
end
end
REviews show. html .erb:
<h6><strong>
Showing "<%= @review.title %>" for
<%= @album.artist %> -
<%= @album.title %>
</strong></h6>
<br>
<% if @album.avatar.attached? %>
<image src="<%=(url_for(@album.avatar))%>%" style="width:350px;height:350px;">
<% end %>
<br>
<br>
<%= link_to "Edit Album", edit_album_path(@album), class: "waves-effect waves-light btn" %>
<%= link_to 'Delete Album', album_path(@album), method: :delete, class: "waves-effect waves-light btn" %>
<br><br>
<hr>
<% unless @review.id.nil? %>
<small>Date written: <%= @review.date.strftime("%d %B %Y") %></small><br>
<small>Written by: <%= @review.user.name %></small><br>
<br>
<strong>Title: <%= @review.title %></strong><br>
<%= @review.content %><br>
<br>
<% if current_user.id == @review.user_id %>
<%= link_to "Edit Review", edit_album_review_path(album_id: @album.id, id: @review.id), class: "waves-effect waves-light btn" %>
<%= link_to 'Delete', album_review_path(album_id: @album.id, id: @review.id), method: :delete, data: { confirm: 'Are you sure?' }, class: "waves-effect waves-light btn" %><br><br>
<% end %>
<br><br>
<% end %>
<br>
<strong>
<%= link_to "Write a New Review", album_path(@album), class: "waves-effect waves-light btn" %></strong>
<br><br>
маршруты рейка:
GET /auth/:provider/callback(.:format) sessions#omniauth
auth_failure_path GET /auth/failure(.:format) redirect(301, /)
signup_path GET /signup(.:format) users#new
POST /signup(.:format) users#create
signin_path GET /signin(.:format) sessions#new
POST /signin(.:format) sessions#create
signout_path GET /signout(.:format) sessions#destroy
logout_path POST /logout(.:format) sessions#destroy
not_found_path GET /not_found(.:format) pages#not_found
album_reviews_path POST /albums/:album_id/reviews(.:format) reviews#create
new_album_review_path GET /albums/:album_id/reviews/new(.:format) reviews#new
edit_album_review_path GET /albums/:album_id/reviews/:id/edit(.:format) reviews#edit
album_review_path GET /albums/:album_id/reviews/:id(.:format) reviews#show
PATCH /albums/:album_id/reviews/:id(.:format) reviews#update
PUT /albums/:album_id/reviews/:id(.:format) reviews#update
DELETE /albums/:album_id/reviews/:id(.:format) reviews#destroy
контроллер страниц для пути not_found
class PagesController < ApplicationController
before_action :current_user
def not_found
end
private
def current_user
super() #looks for same method on what i'm inheriting from (applicationcontroller)
end
end
контроллер альбомов:
class AlbumsController < ApplicationController
before_action :set_current_user, only: [:new, :index, :new, :edit]
before_action :set_album, only: [:show, :edit, :update, :destroy]
before_action :must_login, only: [:new, :show, :create, :edit, :update, :destroy]
def index
@albums = Album.all.order("artist ASC")
end
def show
@review = @album.reviews.build
@review.user = current_user
@reviews = Review.recent #scope shows recent reviews
end
def new
@album = Album.new
@review = @album.reviews.build
end
def create
@album = current_user.albums.build(album_params)
@album.reviews.each { |r| r.user ||= current_user }
if @album.save
redirect_to album_path(@album)
else
render :new
end
end
def edit
end
def update
@album.user_id = current_user.id
@album.avatar.purge # or album.avatar.purge_later
@album.avatar.attach(params[:avatar])
if @album.update(album_params)
redirect_to album_path(@album), notice: "Your album has been updated."
else
render 'edit'
end
end
def destroy
@album.destroy #destroy is for associated objects
@album.avatar.purge
redirect_to albums_path
end
private
def set_current_user
@user = current_user
end
def set_album
@album = Album.exists?(params[:id]) ? Album.find(params[:id]) : nil
redirect_to not_found_path if @album.nil?
end
def album_params
params.require(:album).permit(:artist, :title, :avatar, :user_id, reviews_attributes:[:title, :date, :content])
end
end