Skip to content

Instantly share code, notes, and snippets.

@dsingleton
Last active August 29, 2015 14:17
Show Gist options
  • Save dsingleton/046ec4d5ea7dcdb5051d to your computer and use it in GitHub Desktop.
Save dsingleton/046ec4d5ea7dcdb5051d to your computer and use it in GitHub Desktop.
class Artist < ActiveRecord::Base
extend FriendlyId
friendly_id :name, use: :finders
end
class ArtistsController < ApplicationController
def index
@artists = Artist.all
end
def show
@artist = Artist.find(params[:id])
end
end

Desired behaviour:

Route a resource where the name can be anything, including . and / charts, eg:

  • ...And You Will Know Us by the Trail of Dead
  • GZA/Genius

Issue

Assuming there is the model Artist.create(name: 'GZA/Genius')

Routing request to resourse

If you visit /artists/GZA%2FGenius (manually constructued/properly escaped) then app will route you to the right controller/action, and the value of params[:id] will be GZA/Genius. All is good.

Generating URL from resource

If you call artist_path for that model you'll get a UrlGenerationError:

No route matches {:action=>"show", :controller=>"artists", :id=>#<Artist id: 57, name: "GZA/Genius">} missing required keys: [:id]```

My guess is that it's trying to match the route before escaping `name`, so is trying to match a route like `/artist/GZA/Genius`, which doesn't exist.

(I'm still getting to grips with a lot of Rails, so it's not immediately clear if this is a rails, friendly id or me (likely) issue.)
Rails.application.routes.draw do
constraints(id: /[^\/]+/) do
resources :artists, only: [:index, :show] do
end
end
@dsingleton
Copy link
Author

If constraints is removed then it works as expected, but then artist names with a . in them, eg: /artists/...And%20You%20Will%20Know%20Us%20by%20the%20Trail%20of%20Dead, then param[:id] in the controller is .., as the rest is taken as format.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment