Fix PagesController slug derivation for root and custom paths - #78
Merged
Conversation
Deriving the page slug from request.path broke two cases:
- bunko_page :home, path: "/" resolved to a nil slug and 404ed
- bunko_page :about, path: "about-us" looked up "about-us" instead of
the post's actual slug "about"
Read params[:page] instead, which bunko_page already provides via
defaults: {page: ...}. Route defaults are path parameters and take
precedence over query string params in Rails, so the query string
still cannot override the slug. A format guard 404s if the action is
ever wired up without a :page default.
Updates the template and the committed namespaced dummy controller in
sync, and adds regression tests for root-path pages, custom-path
pages, and query-param override attempts.
Fixes #57
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019SzbpeC8uQM8DmEPpi86vT
Always mark PRs and commits with "Closes #xx" / "Fixes #xx" when working on a ticket so the issue auto-closes on merge. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019SzbpeC8uQM8DmEPpi86vT
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #57
Fixes a regression where
PagesControllerwas unable to render pages configured with root paths (bunko_page :home, path: "/") or custom paths (bunko_page :about, path: "about-us"). The controller was incorrectly deriving the page slug by parsingrequest.pathinstead of reading it from the route'sdefaults: {page: ...}parameter.Key Changes
PagesController slug derivation: Changed from parsing
request.pathto readingparams[:page](which contains the route default). Route defaults take precedence over query string parameters in Rails, preventing query string manipulation attacks while supporting all path configurations.Format validation: Added a format guard (
/\A[a-z0-9-]+\z/) to validate the slug before querying the database, ensuring the controller fails safely if wired up without a:pagedefault.Updated templates: Applied the same fix to the PagesController template (
lib/tasks/templates/controllers/pages_controller.rb.tt) so newly generated controllers use the correct approach.Regression tests: Added comprehensive test coverage for the fixed behavior:
bunko_page :home, path: "/")bunko_page :about, path: "about-us")draw_dummy_routes_withfor dynamically testing route configurationsDocumentation: Updated CHANGELOG.md with the fix details and GitHub issue reference (PagesController slug derivation breaks root path and custom paths #57). Also documented the "Closes #xx" convention for future tickets in CLAUDE.md.
Implementation Details
The fix leverages Rails' route parameter precedence: when a route defines
defaults: {page: "about"}, that value is merged intoparamsand takes precedence over any query string parameter with the same name. This provides both security (prevents path traversal via query strings) and correctness (works with any path configuration).https://claude.ai/code/session_019SzbpeC8uQM8DmEPpi86vT