I'd like to pass a named variable in my rspec call to match
this is the route:
match '/api/get-pairings/:global_id' => 'api#get_pairings', :as => :get_pairings
this is what I have but doesn't work:
it "should get pairings for a specific id" do
{:get => get_pairings_path, :global_id => 1000 }.should route_to(:controller => "api", :action => "get_pairings")
{:get => get_pairings_path, :params => { :global_id => 1000 } }.should route_to(:controller => "api", :action => "get_pairings")
end
Any ideas?
thx in advance
You need to pass the variable to the _path method for the specs to work:
it "should get pairings for a specific id" do
{:get => get_pairings_path(:global_id => 1000) }.
should route_to(:controller => "api",
:action => "get_pairings",
:global_id => "1000")
end
Related
I'm new to Rails and I'm having issues setting up non-resourceful routes with querystring parameters. I am trying to access the following URL:
http://localhost:3000/exerciseLogs?userID=1
When I access this URL, I receive the following error:
No route matches [GET] "/exerciseLogs"
My routes file is defined as follows:
get 'exerciseLogs?userID=:user_id' => 'exercise_logs#index', :defaults => { :format => 'json'}, :as => :get_user_exercise_logs
get 'exerciseLogs/:id' => 'exercise_logs#show', :defaults => { :format => 'json' }, :as => :get_exercise_log
post 'exerciseLogs?userID=:user_id' => "exercise_logs#create", :defaults => { :format => 'json'}, :as => :create_exercise_log
patch 'exerciseLogs/:id' => 'exercise_logs#update', :defaults => { :format => 'json' }, :as => :update_exercise_log_patch
put 'exerciseLogs/:id' => 'exercise_logs#update', :defaults => { :format => 'json' }, :as => :update_exercise_log_put
delete 'exerciseLogs/:id' => 'exercise_logs#destroy', :defaults => { :format => 'json' }, :as => :delete_exercise_log
What is the best way to solve this problem so that my routes actually go the controller?
You don't have to define query string parameters in your route. For details checkout rails routes with query string. So in your case you can do something like this:
get '/exerciseLogs' => 'exercise_logs#index', :defaults => { :format => 'json'}, :as => :get_user_exercise_logs
Define route like this
get 'exerciseLogs/user/:user_id' => 'exercise_logs#user_log_index', :defaults => { :format => 'json'}, :as => :get_user_exercise_logs
and then access it
http://localhost:3000/exerciseLogs/user/1
Define seperate action user_log_index in controller
I try define some translate routes on my rails application. The route change with the subdomain define. So I want this result :
describe "url_for" do
context 'with en' do
it 'brand translate' do
url_for(
:controller => 'boats',
:action => 'index',
:subdomain => :en,
:brand => 'hello',
:only_path => true
).should == '/yacht-charter/brand-hello'
end
end
context 'with fr' do
it 'brand translate' do
url_for(
:controller => 'boats',
:action => 'index',
:subdomain => :fr,
:brand => 'hello',
:only_path => true
).should == '/location-bateau/marque-hello'
end
end
end
Like you can see the only change between both url_for params is the subdomain. I try :
constraints => :subdomain => :en do
match '/yacht-charter/brand-:brand' => 'boats#index', :as => 'en_brand_search'
end
constraints :subdomain => :fr do
match '/location-bateau/marque-:brand' => 'boats#index', :as => 'fr_brand_search'
end
But all the time it's the first route define it use. the second is never define.
How Can i do that. It's a rails bug or not ?
Not sure about it being a rails bug or not, but have a look at https://github.com/francesc/rails-translate-routes it works well and may help achieving this.
I'm upgrading from Rails 2 to 3.1, and having some problems with route globs. We had working globbed routes in Rails 2 that were like this:
map.connect '/foo/*bar', :controller => 'foo', :action => 'index'
A GET to '/foo/one/two' would give the following parameters at the controller side:
{
:controller => 'foo',
:action => 'index',
:bar => ['one, 'two']
}
Empty globs were fine, so we could also do GET '/foo' and get:
{
:controller => 'foo',
:action => 'index',
:bar => []
}
Upgrading to Rails 3.1, there are a few differences:
match '/foo/*bar', :to => 'foo#index'
The globbed route segment comes through as a string instead of an array, but I've dealt with that in my code. No problem there. A GET '/foo/one/two' gives:
{
:controller => 'foo',
:action => 'index',
:bar => 'one/two'
}
The problem comes when I have nothing in the glob segment. GET '/foo' is no longer routable, and if I try to generate the URL from parameters, it cannot generate. Basically, if params[:bar] is an empty string, it just completely fails to route.
This is awkward in my app, as it depends on the Rails 2.3 behaviour to produce sensible URLs. We also have instances where the glob is mid-path:
match '/foo/*bar/show', :to => 'foo#show'
This means that we had paths like:
/foo/one/two/show
/foo//show
Is there any way to get the routing engine to handle the empty strings as it used to in Rails 2.3? I've tried adding extra routes for the empty-glob version, which works in the simple trailing-glob case, but in the case where the glob is mid-path, it doesn't.
I get tests passing with these routes:
Your::Application.routes.draw do
match '/foo/', :to => 'foo#index', :bar => ''
match '/foo//show', :to => 'foo#show', :bar => ''
match '/foo/*bar/show', :to => 'foo#show'
match '/foo/*bar', :to => 'foo#index'
end
Sequence is important!
Ambiguity: Is /foo/show meant to "show foo-index", or do "index for bar=show"?
Test:
require_relative '../test_helper'
class FooRoutingTest < ActionController::TestCase
test 'root index' do
assert_recognizes({:controller => 'foo', :action => 'index', :bar => ''}, '/foo')
assert_recognizes({:controller => 'foo', :action => 'index', :bar => ''}, '/foo/')
end
test 'root show' do
assert_recognizes({:controller => 'foo', :action => 'show', :bar => ''}, '/foo/show')
assert_recognizes({:controller => 'foo', :action => 'show', :bar => ''}, '/foo//show')
end
test 'wildcard index' do
assert_recognizes({:controller => 'foo', :action => 'index', :bar => 'hello'}, '/foo/hello')
assert_recognizes({:controller => 'foo', :action => 'index', :bar => 'hello/more'},'/foo/hello/more')
end
test 'wildcard show' do
assert_recognizes({:controller => 'foo', :action => 'show', :bar => 'wow'}, '/foo/wow/show')
assert_recognizes({:controller => 'foo', :action => 'show', :bar => 'wow/more'}, '/foo/wow/more/show')
end
end
Fairly certain I'm doin' this wrong. How can I DRY it up?
controller 'foo' do
get 'foo/bar', :action => 'bar', :as => 'foo_bar'
post 'foo/bar', :action => 'bar', :as => 'foo_bar'
post 'foo/baz', :action => 'baz', :as => 'foo_baz'
end
rake routes shows the same routing with this:
get 'foo/bar'
post 'foo/bar'
post 'foo/baz'
Given a couple of cities in the DB:
City.first.attributes => {:id => 1, :name => 'nyc'}
City.last.attributes => {:id => 2, :name => 'boston'}
And a route like:
match '/:city/*dest' => 'cities#do_something', :constraints => {:city => /#{City.all.map{|c| c.name}.join('|'}/}
(so the constraints should evaluate to: /nyc|boston/)
And a spec:
it "recognizes and generates a route for city specific paths" do
{ :put => '/bad-city/some/path' }.should route_to({:controller => "cities", :action => "do_something", :dest => 'some/path', :city => 'bad-city'})
end
I would expect a failure. But it passes.
Likewise:
it "doesn't route bad city names" do
{ :put => '/some-bad-city/some/path' }.should_not be_routable
end
Here I expect it to pass, but it fails.
It seems the constraint is being ignored in the specs, since the matching cities have the same behavior as the bad ones.
Is this a known issue, or am I missing something that I need to do?
This approach works:
In routes.rb
match '/:city/*destination' => 'cities#myaction', :constraints => {:city => /#{City.all.map{|c|c.slug}.join('|')}/}
In the spec:
describe "routing" do
before(:each) do
#mock_city = mock_model(City, :id => 42, :slug => 'some-city')
City.stub!(:find_by_slug => #mock_city, :all => [#mock_city])
MyApp::Application.reload_routes!
end
it "recognizes and generates a route for city specific paths" do
{ :get => '/some-city/some/path' }.should route_to({:controller => "cities", :action => "myaction", :destination => 'some/path', :city => 'some-city'})
end
it "rejects city paths for cities that don't exist in the DB" do
{ :get => '/some-bad-city/some/path' }.should_not be_routable
end
end
Lastly, I added an observer to reload routes whenever the cities table changes.
When you specify constraints, you must include the parameter to constrain:
match '/:city/*dest' => 'cities#do_something', :constraints => { :city => /nyc|boston|philly/ }