Create a simple route - ruby-on-rails

I am having problems create the right route. I want to pass in the id of the element that i am working on but it does not look right.
my route looks like
resources :accounts
match 'account-audit' => 'accounts#audited',:as => :accountaudit
and when i do rake routes i get
accounts GET /accounts(.:format) accounts#index
POST /accounts(.:format) accounts#create
new_account GET /accounts/new(.:format) accounts#new
edit_account GET /accounts/:id/edit(.:format) accounts#edit
account GET /accounts/:id(.:format) accounts#show
PUT /accounts/:id(.:format) accounts#update
DELETE /accounts/:id(.:format) accounts#destroy
accountaudit /account-audit(.:format) accounts#audited
when i go to the page the link looks
localhost:3000/account-audit.3
and it should look like
localhost:3000/account/3/audit
how do i make my route do what i need it to do?

You need to declare routes like this
resources :accounts do
get :audit, on: :member, as: :accountaudit
end
This will generate links like localhost:3000/accounts/account_id/audit. Check this stackoverlfow question to learn about member and collection routes.

What it looks like you are trying to do is a nested routes this will give you the restful routes for audit inside of accounts
resources :accounts do
resources :audit
end

Related

Rails Routes: change resource identifier path name? always use params[:model_id] instead of params[:id]

I'm not sure how to ask this question...
But i'm working with nested ROUTES as shown below.
I like knowing that the Business ID can always be found using params[:business_id], obviously except for the actual business controller which requires me to use params[:id].
Is there a way to change the route resource id param to always be :business_id instead of having to be like Business.find(params.values_at(:business_id, :id).first)??
business_exports GET /businesses/:business_id/exports(.:format) businesses/exports#index
POST /businesses/:business_id/exports(.:format) businesses/exports#create
new_business_export GET /businesses/:business_id/exports/new(.:format) businesses/exports#new
edit_business_export GET /businesses/:business_id/exports/:id/edit(.:format) businesses/exports#edit
business_export GET /businesses/:business_id/exports/:id(.:format) businesses/exports#show
PATCH /businesses/:business_id/exports/:id(.:format) businesses/exports#update
PUT /businesses/:business_id/exports/:id(.:format) businesses/exports#update
DELETE /businesses/:business_id/exports/:id(.:format) businesses/exports#destroy
business_replenishments GET /businesses/:business_id/replenishments(.:format) businesses/replenishments#index
business_offer_prices GET /businesses/:business_id/offer_prices(.:format) businesses/offer_prices#index
POST /businesses/:business_id/offer_prices(.:format) businesses/offer_prices#create
business_unmatched_listings GET /businesses/:business_id/unmatched_listings(.:format) businesses/unmatched_listings#index
POST /businesses/:business_id/unmatched_listings(.:format) businesses/unmatched_listings#create
business_profit_loss_reports GET /businesses/:business_id/profit_loss_reports(.:format) businesses/profit_loss_reports#index
businesses GET /businesses(.:format) businesses#index
POST /businesses(.:format) businesses#create
new_business GET /businesses/new(.:format) businesses#new
edit_business GET /businesses/:id/edit(.:format) businesses#edit
business GET /businesses/:id(.:format) businesses#show
PATCH /businesses/:id(.:format) businesses#update
PUT /businesses/:id(.:format) businesses#update
DELETE /businesses/:id(.:format) businesses#destroy
If you're using Rails 4+ you can do this using the param option in the resources method
Overriding Route Parameters
Assuming you have code that looks like
resources :businesses
You can add an argument as such
resources :businesses, param: :business_id
Which should generate routes
businesses GET /businesses(.:format) businesses#index
POST /businesses(.:format) businesses#create
new_business GET /businesses/new(.:format) businesses#new
edit_business GET /businesses/:business_id/edit(.:format) businesses#edit
business GET /businesses/:business_id(.:format) businesses#show
PATCH /businesses/:business_id(.:format) businesses#update
PUT /businesses/:business_id(.:format) businesses#update
DELETE /businesses/:business_id(.:format)
UPDATE
Since you're generating these routes using the same nested resources you'll have to do the following
resources :businesses, param: :business_id
resources :businesses, only: [] do
resources :exports
...
end
A more clean way is to use the member
resources :businesses, param: :business_id do
member do
resources :exports
...
end
end

Rails routes index for nested resource

I'm searching a reason why rake routes doesn't match the index path of my nested resource.
Here is my code:
namespace :api do
resources :photos do
resource :comments
end
end
Here is the result of the command: rake routes | grep comment
batch_action_admin_user_comments POST /admin/user_comments/batch_action(.:format) admin/user_comments#batch_action
admin_user_comments GET /admin/user_comments(.:format) admin/user_comments#index
POST /admin/user_comments(.:format) admin/user_comments#create
new_admin_user_comment GET /admin/user_comments/new(.:format) admin/user_comments#new
edit_admin_user_comment GET /admin/user_comments/:id/edit(.:format) admin/user_comments#edit
admin_user_comment GET /admin/user_comments/:id(.:format) admin/user_comments#show
PATCH /admin/user_comments/:id(.:format) admin/user_comments#update
PUT /admin/user_comments/:id(.:format) admin/user_comments#update
DELETE /admin/user_comments/:id(.:format) admin/user_comments#destroy
admin_comments GET /admin/comments(.:format) admin/comments#index
POST /admin/comments(.:format) admin/comments#create
admin_comment GET /admin/comments/:id(.:format) admin/comments#show
api_photo_comments POST /api/photos/:photo_id/comments(.:format) api/comments#create
new_api_photo_comments GET /api/photos/:photo_id/comments/new(.:format) api/comments#new
edit_api_photo_comments GET /api/photos/:photo_id/comments/edit(.:format) api/comments#edit
GET /api/photos/:photo_id/comments(.:format) api/comments#show
PATCH /api/photos/:photo_id/comments(.:format) api/comments#update
PUT /api/photos/:photo_id/comments(.:format) api/comments#update
DELETE /api/photos/:photo_id/comments(.:format) api/comments#destroy
I tried to add only: [:create, :index] to my comments resource but only the create route is visible.
According to the documentation about nested-resources I don't understand what's happening.
Thank you for your help.
It's because you are using a singular resource (resource :comments)
From the docs:
Sometimes, you have a resource that clients always look up without
referencing an ID. For example, you would like /profile to always show
the profile of the currently logged in user. In this case, you can use
a singular resource to map /profile (rather than /profile/:id) to the
show action
You'll need to use the standard resources method to get this working (resource omits the index action):
#config/routes.rb
namespace :api do
resources :photos do
resources :comments
end
end
My mistake. A "S" was missing on my resource.
namespace :api do
resources :photos do
resources :comments
end
end
Now it works.

Rails nested resources and paths

in my routes.rb I have:
resources :boards do
resources :items
end
now in boards#show I want to show a link to http://mysite/baord/:board_id/items/new, running rake routes I get:
new_board_item GET /boards/:board_id/items/new(.:format) items#new
and so I should be able to use new_board_item_path but this works only from site.com/board/:board_id/items but i want to use this link in boards#show action but it tells me that:
No route matches {:action=>"new", :controller=>"items"}
while that's not true!
Pass the parent resource into the path, so in this case:
new_board_item_path(#board)

Rails 3 routing and namespaces

I want to have a namespaced controller called 'portal'.
in this will be nested resources such as companies and products.
I'd like routes like :
/portal/:company_id/product/:id to work
I can get
/portal/company/:company_id/product/:id to work but would like to eliminate the 'company' in the url
Hope that is clear. Please keep in mind that I need the namespaced module portal to exist.
I think you could use scope to achieve what you want. Perhaps like this:
namespace "portal" do
scope ":company_id" do
resources :products
end
end
That will generate the following routes:
portal_products GET /portal/:company_id/products(.:format) {:action=>"index", :controller=>"portal/products"}
POST /portal/:company_id/products(.:format) {:action=>"create", :controller=>"portal/products"}
new_portal_product GET /portal/:company_id/products/new(.:format) {:action=>"new", :controller=>"portal/products"}
edit_portal_product GET /portal/:company_id/products/:id/edit(.:format) {:action=>"edit", :controller=>"portal/products"}
portal_product GET /portal/:company_id/products/:id(.:format) {:action=>"show", :controller=>"portal/products"}
PUT /portal/:company_id/products/:id(.:format) {:action=>"update", :controller=>"portal/products"}
DELETE /portal/:company_id/products/:id(.:format) {:action=>"destroy", :controller=>"portal/products"}
Edit: Accidentally used resource instead of resources. Fixed now.
You can customize the routes to nearly whatever you want if you spell them out directly, like this:
match '/portal/:company_id/product/:id', :to => 'companies_products#show'
The :to part specifies the controller and action to use, something that should match what you have in your routes now. If you're not sure what that is, rake routes will tell you its specific interpretation.

Rails 3 restful route problem

The following link works in my app:
<%= link_to "invitation", :controller => :invitations, :action => :index %>
To follow restful conventions i changed the link to:
<%= link_to "invitation", index_invitation_path %>
The error that i get is:
undefined local variable or method `index_invitation_path'
Rake routes yields:
invitations GET /invitations(.:format) {:controller=>"invitations", :action=>"index"}
The page name is index.html.erb. The model is invitation.rb. The controller is invitation_controller.rb. Routes has resources :invitations. What am i missing?
Thanks!
Assuming you have the routing correct:
resources :invitations
Then the correct helper for the index action (with the url /invitations.html) is
invitations_path
You can see more information by running rake routes. It will display text like the following:
lists GET /lists(.:format)
{:action=>"index", :controller=>"lists"}
POST /lists(.:format)
{:action=>"create", :controller=>"lists"}
new_list GET /lists/new(.:format)
{:action=>"new", :controller=>"lists"}
edit_list GET /lists/:id/edit(.:format)
{:action=>"edit", :controller=>"lists"}
list GET /lists/:id(.:format)
{:action=>"show", :controller=>"lists"}
PUT /lists/:id(.:format)
{:action=>"update", :controller=>"lists"}
DELETE /lists/:id(.:format)
{:action=>"destroy", :controller=>"lists"}
root /(.:format)
{:controller=>"lists", :action=>"index"}
The above was from a route of my own (for a model called List). The route helper method is shown immediately before the HTTP method. You have to remember to append the _path to each helper method. For example the helper methods I could use are:
list_path(list)
edit_list_path(list)
new_list_path
lists_path
You'll need a route in your routes.rb file that defines a mapping to the invitations controller and the index action.
Typically this is created with a resources call
resources :invitations
Which creates several default routes, which you can see by running rake routes.
For single resources, you can also define it using a match call
match "invitations/:id" => "invitations#index", :as => index_invitation
The rails site has a great resource on routing that provides all the details: Routing from the Outside In
Update: Based on your updated question, your route includes an invitaions (notice the trailing 's') route - nothing with index or invitation. The index_ prefix is generated by the resources call when it creates the default routes for :invitations.
It looks like you've defined a custom get mapping for an invitation. While this may technically work, if you're aim is to support restful routes, use the resources method. And have a read of the Routing guide from rails it's very easy to follow and quite detailed.
type rake routes in your console and look at listing of available routes. Seems to be there is no such route index_invitation_path? maybe it named differently
I think you need "invitations_controller.rb" to contain InvitationsController. Plural.

Resources