I created an ActiveAdmin controller:
ActiveAdmin.register InboundNumber do
controller do
def create
whatever
end
and I can see the route after run the "$ rails routes" command:
POST /admin/inbound_numbers(.:format) admin/inbound_numbers#create
but there is no "named route" part, I mean the part produced by the ":as" symbol in the routes file. I like to call the path by its named route in my Rspec tests.
I can add it manually in the config/routes.rb file:
post '/admin/inbound_numbers', to: 'admin/inbound_numbers#create', as: :admin_inbound_number_create
but that duplicates the route, also I'm pretty sure that is not the "ActiveAdmin way", is there a way to add the ":as" (named route) part?
Related
I created a new controller and actions for a new model. I have setup the rails routes like below:
namespace :api do
namespace :v1 do
resources :universes
end
end
I am trying to write up some RSpec tests using the path helpers (ex. get(api_v1_universe_path(universe.id))). When I go to look at the post route for invites the route is missing the prefix as shown here:
POST /api/v1/universes(.:format) api/v1/universes#create
new_api_v1_universe GET /api/v1/universes/new(.:format) api/v1/universes#new
edit_api_v1_universe GET /api/v1/universes/:id/edit(.:format) api/v1/universes#edit
api_v1_universe GET /api/v1/universes/:id(.:format) api/v1/universes#show
I know I can manually add a line like post "/", to: "universe#create", as: "create" to get a route with a prefix. However, I wanted to know what was causing prefix to be missing from the post route so I can possibly fix that instead?
I want to create a link to a named route
My routes.db have the following rule
match '/tablero', to: 'tablero#index',via: 'get' , as: 'tablero_main'
I can see the route using rake routes
tablero_main GET /tablero(.:format) tablero#index
But when i use the link_to as follows i get the "undefined local variable or method `tablero_main'" error.
<%= link_to "Tablero",tablero_main %>
Is there anything else i am missing?
You need to append path to the method name, like so:
<%= link_to "Tablero", tablero_main_path %>
Routes
To help you further, you'll need to also consider the role of resources in your routes
As Rails uses a resourceful routing infrastructure, every route you create should be based around a resource. In your case:
#config/routes.rb
resources :tablero, only: :index #-> domain.com/tablero
Admittedly, this will give you the path tablero_index_path, rather than tablero_main_path, but it ensures your routes are not only DRY, but also extensible. Nothing worse than having 100's of "match routes in a route file.
--
Helpers
After that, remember to use the correct route_path helper:
Each "route" path is basically just a helper method (which builds a URL for you). When using link_to, you need to reference the path helper directly. You didn't do this, which lead Rails to come back with the undefined method error
This question already has answers here:
:as in rails routes.rb
(3 answers)
Closed 8 years ago.
In ruby on rails, what does the "as:" do in route?
Example: http://guides.rubyonrails.org/routing.html 1.2
You can also generate paths and URLs. If the route above is modified to be:
get '/patients/:id', to: 'patients#show', as: 'patient'
and your application contains this code in the controller:
#patient = Patient.find(17)
and this in the corresponding view:
<%= link_to 'Patient Record', patient_path(#patient) %>
In routes as: option is used to make url or path helpers for that particular route. If you look at your route:
get '/patients/:id', to: 'patients#show', as: 'patient'
You have specified as: 'patient' which will enable rails to make patient_path and patient_url helpers
patient_path will give you /patient/:id and patient_url will give you domain/patient/:id
If you run rake routes in your terminal it will list all the routes of your application with there corresponding helper methods. For details checkout path and url helpers
It defines what the route helpers will look like:
patient_path(#patient)
It's the path name which is generated from your routes
For example, if you have the following:
#config/routes.rb
resources :bones, as: :skeleton
You'll get routes as skeleton_path for the bones controller
--
You have to remember that Rails' routing structure is based around "resources" (handled by controllers). Your routes, therefore, should be structured around the controllers your application has
If you want to change the "name" of a particular controller path helper (from bones to skeleton for example), you'll be able to use the as option
I try to add new controller and model use name foo and foos_controller, hope foos_path can redirect. Doesn't work.
A origin code here (working):
href="<%= contacts_path %>"
After I add new controller and model follow name convention I try use the same (Not working):
href="<%= foos_path %>"
And this contacts_path is not defined anywhere else in rb project.
what does xxxx_path mean and how to use it?
Rails follows convention to handle roots of application
when we execute this command
rails g scaffold foo
it generates routes along with your model, controller and views.
it generates a line in routes.rb as
resources :foo
this line makes you access all the actions of your controller
for example:
foos_path: # redirects you to the index page of your foos controller
new_foo_path: # redirects you to the create page of your foos controller etc.,
please go through this link for reference: http://guides.rubyonrails.org/routing.html
If you go to your terminal and type rake routes, it will list your currently defined routes. On the left side you'll have their prefix. For example, contacts might route to the index action in ContactsController.
The path suffix is a way of referring to those routes inside your code.
If foos_path is giving you an error, that means you either have not yet defined that route for the resource, or you have but it is called something else (if you defined it manually with the as: option).
More info can be found in the Rails Guide for Routing.
You'll be best reading up on this documentation
Path Helpers
Basically, the path_helpers of Rails are designed to help you define routes for your links etc in the most efficient way possible.
The bottom line is the path helper will take routes defined in your config/routes.rb and then allow you to call them dynamically - IE:
#config/routes.rb
resources :photos #-> photos_path
The path names are typically from your controllers, allowing you to route to the various actions inside them. As Rails is built around being a resourceful structure, it will by default create routes for the "standard" controller actions:
link_to
In order to use the path helpers effectively, you'll be best using the rake routes command in cmd, or by simply typing an invalid url into your app's address bar
I notice you're using the standard HTML <a href=""> tag in your question. You'll be much better suited to using the link_to helper of Rails:
<%= link_to "text", your_path %>
I have a problem where Rails is searching an additional subdirectory based on the controller path. Is there a way to get rails to stop searching one extra subdirectory? I kind of like the directory structure that I have now. Here's the details:
Rails will return this error message. As you can see, it's going for v1 twice:
Template is missing
Missing template api/v1/v1/print
I have a controller in app/controllers/api/v1/v1_controller.rb and a view in app/views/api/v1/print.html.erb
The specific route in config/routes.rb is (semi-truncated):
namespace :api do
scope module: :v1 do
match "v1/print",
:to => "v1#print"
end
end
Based on the routes, it looks OK. Rake routes show this:
api_v1_print GET|POST /api/v1/print(.:format) api/v1/v1#print {:format=>"html"}
Why is it going one directory too deep?
The problem is that Rails assumes there's a subdirectory per each controller. The duplication is formed since you have v1 in the module and in the controller name. I wouldn't go against Rails' conventions. Instead I would change the name of the controller to API controller (or something similar) and put the templates under the directory called API.
In case you still want to do this, simply use render within your print action and specify the exact file you'd like to use (see here)
just remove the v1 from the match, like this:
namespace :api do
scope module: :v1 do
match "print",
:to => "v1#print"
end
end
EDIT
sorry, the problem is in your template folder.
app/views/api/v1/print.html.erb
app/views/(namespace)/(module)/(action) <- you have forgoten the controller
the right one would be:
app/views/api/v1/v1/print.html.erb