I'm working on a project where I have a Rails application (rails 4.2.5) that incorporates a gem, which is a Rails Engine. I'm currently working on moving the route definitions from the main application to the gem. I have a controller test that exercises one of the routes. The test fails when I move the route definition over to the gem, but the route looks the same.
My gem is called CallCenter, and includes a controller called call_center_controller (with an action called reports_tab). This is what routes.rb looks like in the Rails app:
...
mount CallCenter::Engine, at: "/call_center", as: 'call_center_urls'
get '/call_center/reports_tab' => 'call_center/call_center#reports_tab'
...
This is what I get running rake routes:
❯❯❯ rake routes | grep reports_tab
call_center_reports_tab GET /call_center/reports_tab(.:format) call_center/call_center#reports_tab
At this point controller test runs without errors. But when I move the route definition to the routes.rb file in the gem:
get 'call_center/reports_tab' => 'call_center#reports_tab'
(note that the route now points to 'call_center#reports_tab' instead of 'call_center/call_center#reports_tab', because the main application mounts the engine at /call_center)
Here is my output from rake routes, (still running from the main app's directory)
❯❯❯ rake routes | grep reports_tab
call_center_reports_tab GET /call_center/reports_tab(.:format) call_center/call_center#reports_tab
This is identical to the route reported by rake routes when it was defined in the main app, but now my test fails with the following message:
Failure/Error: get :reports_tab
ActionController::UrlGenerationError:
No route matches {:action=>"reports_tab", :controller=>"call_center/call_center"}
As far as I can tell the route is defined, and looks exactly the same as it did before when it was in the routes file for the main app. What am I missing?
I found the answer - I needed to add this to my controller spec to tell it to look at the routes provided by the Engine:
routes { CallCenter::Engine.routes }
Related
I'm really confused because some of my routes when I use controllername_index_url are routing to /controllername/index while other's are routing to /controllername.
The main difference is that when I generate the index method when I create the controller through the command line
rails g controller controllerName1 index
the route then becomes /controller_name_1/index.
When I create the index manually after creating the controller, it becomes /controllername2
In my config/routes I am including my controllers like:
Rails.application.routes.draw do
resources :controller_name_1
resources :controller_name_2
end
And when I do a rails routes my routes look like
controller_name_1_index GET /controller_name_1/index(.:format) controlle_name_1#index
controller_name_2_index GET /controller_name_2(.:format) controller_name_2#index
Why is it automatically adding the routes differently? How can I make it so that regardless of it I generate the index methods on the command line or add them in after the fact, controller_name_index_url routes are always the same format? (I'm using ruby 2.4.0 and rails 5.1.2 if that's helpful)
When you create an action at the command line a route is automatically generated for it.
$ rails g controller ones index
create app/controllers/ones_controller.rb
route get 'ones/index' # route helper is ones_index
...
The behavior is totally agnostic of the action you're creating. It could be any action name and Rails will do the same thing
$ rails g controller twos smorgas
create app/controllers/twos_controller.rb
route get 'twos/smorgas' # route helper is twos_smorgas
...
When you add resources to your routes
resources :ones
you automatically get all of the default REST route helpers and routes, no matter how you create any of the REST actions.
$ rake routes
ones GET /ones(.:format) ones#index
POST /ones(.:format) ones#create
new_one GET /ones/new(.:format) ones#new
edit_one GET /ones/:id/edit(.:format) ones#edit
one GET /ones/:id(.:format) ones#show
PATCH /ones/:id(.:format) ones#update
PUT /ones/:id(.:format) ones#update
DELETE /ones/:id(.:format) ones#destroy
It's best to stick with Rails convention and use the default route helpers
ones_url
ones_path
# not ones_index_url
See this SO question if you want to disable the automatic route generation
In a Rails and Ember project, I decided to use EmberCLI Rails because I want to do integration tests with Capybara and all my favorite testing gems.
I installed it and it works when I go to the home page.
I added routes on ember like this :
import Ember from 'ember'
import config from './config/environment'
Router = Ember.Router.extend(location: config.locationType)
Router.map ->
#resource 'users'
export default Router
When I go on http://localhost:3000/users, I have a no route matches error. I understand why this is happening, Rails does not load routes of embers. Is there a solution to do it or is it just impossible with EmberCli-Rails for now?
Your Rails app needs a wildcard route so it knows to handle those requests through your Ember app controller. Can you try adding a route in routes.rb:
get '/:all', to: "ember#index"
substituting ember#index with whatever you have set up as the controller and action for your Ember app.
On visiting localhost:3000 I'm getting the typical:
No route matches [GET] "/"
I don't want to see that error screen. I already trying with localhost:3000/passbook/v1/passes/ but still nothing so I typed:
rake routes
and got this output:
passbook GET /passbook/v1/passes/:pass_type_identifier/:serial_number(.:format) passbook/passes#show {:pass_type_identifier=>/([\w\d]\.?)+/}
GET /passbook/v1/devices/:device_library_identifier/registrations/:pass_type_identifier(.:format) passbook/registrations#index {:pass_type_identifier=>/([\w\d]\.?)+/}
POST /passbook/v1/devices/:device_library_identifier/registrations/:pass_type_identifier/:serial_number(.:format) passbook/registrations#create {:pass_type_identifier=>/([\w\d]\.?)+/}
DELETE /passbook/v1/devices/:device_library_identifier/registrations/:pass_type_identifier/:serial_number(.:format) passbook/registrations#destroy {:pass_type_identifier=>/([\w\d]\.?)+/}
passbook_log POST /passbook/v1/log(.:format) passbook/logs#create
How can I resolve this?
You haven't told Rails yet what your root path is. You can do this by saying
root to: "passes#show"
in config/routes.rb. Afterwards, you should see this in the rake routes output:
root / passes#show
Then it should work!
Additional tip
This may be over the top for your question, but following the test driven approach you should write a spec first:
describe "custom routing" do
it "shows the root page" do
get("/").should route_to("passes#show")
end
end
Use RSpec for this. Run the test in your console before having written the route itself by doing $ rspec and see the test fail at the right place.
Then, implement the routes above, run the test again—it should work. This way you ensure that you have written just enough code to meet your requirements, and that it is really the code you have written that lets you access the root path.
Is this a rails app you inherited from someone else?
Given the rake routes output you have provided, a url like localhost:3000/passbook/v1/passes/<EXAMPLE_PASS_TYPE_IDENTIFIER>/<EXAMPLE_SERIAL_NUMBER> should work based on the rule in the first line of the output, provided you replace EXAMPLE_PASS_TYPE_IDENTIFIER and EXAMPLE_SERIAL_NUMBER with valid values from the database.
The error message No route matches [GET] "/ for localhost:3000 is because there is no mapping for root in rake routes output.
You could modify the config/routes.rb file and add the following line at the bottom of the file:
root :to => "<controller_name>#<action_name>"
Eg:
root :to => "passbook/index"
if there is a passbooks_contoller.rb with a index method.
Best to learn more about rails routing from the documentation. Even better go through the Rails Tutorial to get a good understanding of the rails framework.
I want to list all defined helper path functions (that are created from routes) in my rails 3 application, if that is possible.
Thanks,
rake routes
or
bundle exec rake routes
Update
I later found that, there is an official way to see all the routes, by going to http://localhost:3000/rails/info/routes. Official docs: https://guides.rubyonrails.org/routing.html#listing-existing-routes
Though, it may be late, But I love the error page which displays all the routes. I usually try to go at /routes (or some bogus) path directly from the browser. Rails server automatically gives me a routing error page as well as all the routes and paths defined. That was very helpful :)
So, Just go to http://localhost:3000/routes
One more solution is
Rails.application.routes.routes
http://hackingoff.com/blog/generate-rails-sitemap-from-routes/
rails routes | grep <specific resource name>
displays resource specific routes, if it is a pretty long list of routes.
Trying http://0.0.0.0:3000/routes on a Rails 5 API app (i.e.: JSON-only oriented) will (as of Rails beta 3) return
{"status":404,"error":"Not Found","exception":"#>
<ActionController::RoutingError:...
However, http://0.0.0.0:3000/rails/info/routes will render a nice, simple HTML page with routes.
CLI
updated for version 6
To list all existing routes you'll want to run the command:
bundle exec rails routes
bundle exec will
Execute a command in the context of the bundle
and rails routes will
list all of your defined routes
Example
If you have your resource route declared like so:
resource :credential, only: [:new, :create, :destroy]
Then it's helpful to pipe the output so you can grep for your specific resource.
For example:
In this rails app:
https://github.com/ryanb/govsgo/blob/master/app/views/authentications/index.html.erb
There is a form that as 'new_user_path'
Where is the definition for this symbol? Is it a symbol??
Just confused as I downloaded the source, and searched for 'new_user' and couldn't find any reference to it.
It is located in routes.rb with the name new_user. It's not in this file.
EDIT:
routes.rb is here: https://github.com/ryanb/govsgo/blob/master/config/routes.rb.
But this is rails 3 format and I don't see new_user route. Run rake routes and you should see one called new_user.
I haven't downloaded the codebase but if you run rake routes you will probably see a route definition for new_user.
If you check config/routes.rb you will see the line resources :users. This creates several paths (like edit_user_path, users_path etc.), amongst them new_user_path.
You can see all generated path by running $ rake routes.