Skip to content

Instantly share code, notes, and snippets.

@1syo
Last active August 29, 2015 14:17
Show Gist options
  • Save 1syo/378aa00ae72d5f10b692 to your computer and use it in GitHub Desktop.
Save 1syo/378aa00ae72d5f10b692 to your computer and use it in GitHub Desktop.
# config/initializers/constraints.rb
class Home
def matches?(req)
req.params[:id] #=> nil
# .....
end
end
# app/controllers/home_controller.rb
class HomeController < ApplicationController
def show
render text: params[:id]
end
end
# spec/routing/home_routing_spec.rb
require 'spec_helper'
describe 'routes to the home controller' do
it 'routes /home/1 to home#show' do
get('/home/1').should route_to controller: 'home', action: 'show', id: '1'
end
end
# config/routes.rb
Spike::Application.routes.draw do
get "home/:id" => 'home#show', constraints: Home.new
end
@1syo
Copy link
Author

1syo commented Mar 17, 2015

  • rake spec すると constraints .rbreq.params[:id] #=> nil になる
  • rails server して http://localhost:3000/home/1 すると req.params[:id] #=> "1" になる

@1syo
Copy link
Author

1syo commented Mar 17, 2015

gem 'rails', '3.2.21'

@1syo
Copy link
Author

1syo commented Mar 17, 2015

@1syo
Copy link
Author

1syo commented Mar 17, 2015

4.2.0 は OK

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