Skip to content

Commit

Permalink
Use the request slug instead of the whole path (#708)
Browse files Browse the repository at this point in the history
The current example code only works correctly for the `#show` action. If the user is using `find_post` for anything other than `#show`, it will re-direct a URL for `#edit` using the new slug to the `#show` for the new slug, making it impossible to edit (or update or destroy)

Note that this will redirect from `/posts/old_slug/edit` to `posts/new_slug`, so from `#edit` to `#show`. The code I implemented actually checks to see if the action_name is 'edit', and if it is, re-direct to the correct edit path. For sake of simplicity, I think it's fine to have the docs re-direct to the `#show`.
  • Loading branch information
cllns authored and parndt committed Nov 5, 2019
1 parent a9b40f0 commit 1381c35
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/friendly_id/history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ def find_post
@post = Post.friendly.find params[:id]
# If an old id or a numeric id was used to find the record, then
# the request path will not match the post_path, and we should do
# a 301 redirect that uses the current friendly id.
if request.path != post_path(@post)
# the request slug will not match the current slug, and we should do
# a 301 redirect to the new path
if params[:id] != @post.slug
return redirect_to @post, :status => :moved_permanently
end
end
Expand Down

0 comments on commit 1381c35

Please sign in to comment.