Last active
September 29, 2015 12:07
-
-
Save mcelaney/1598216 to your computer and use it in GitHub Desktop.
Note on altering the show action using FriendlyId with scaffolds and RSpec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Note on altering the show action using FriendlyId with scaffolds and RSpec | |
# Ran into an issue while following http://railscasts.com/episodes/314-pretty-urls-with-friendlyid | |
# GET /articles/1 | |
# GET /articles/1.json | |
def show | |
@article = Article.find(params[:id]) | |
if request.path != article_path(@article) | |
redirect_to @article, status: :moved_permanently | |
end | |
respond_to do |format| | |
format.html # show.html.erb | |
format.json { render json: @article } | |
end | |
end | |
# Throws a failure like this: | |
# | |
# 1) ArticlesController GET show assigns the requested article as @article | |
# Failure/Error: get :show, {:id => article.to_param}, valid_session | |
# AbstractController::DoubleRenderError: | |
# Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return". | |
# # ./app/controllers/articles_controller.rb:21:in `show' | |
# # ./spec/controllers/articles_controller_spec.rb:55:in `block (3 levels) in <top (required)>' | |
# Quick fix I'm sure could be made more eloquent | |
# GET /articles/1 | |
# GET /articles/1.json | |
def show | |
@article = Article.find(params[:id]) | |
if request.path != article_path(@article) | |
redirect_to @article, status: :moved_permanently | |
else # <= Prevents error | |
respond_to do |format| | |
format.html # show.html.erb | |
format.json { render json: @article } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment