Rails _path helper generating path with format not id - ruby-on-rails-3

In my routes.rb I have:
resources :aquariums do
resources :management_roles
resources :graphs
resources :animals
end
get 'aquarium', to: 'aquariums#show', :as => :aquarium
The reason for the last get is I have the notion of "current aquarium" in my app. If, say, current_aquarium is set to 1, then in my controller's 'show' action '/aquarium' gets the same rendering as '/aquariums/1' with code like
#aquarium_id = params[:id] || current_aquarium.id
Now, and I'm assuming this is thanks to this different routing, this code:
<%= link_to aquarium.name, aquarium %>
or
<%= link_to aquarium.name, aquarium_path(aquarium) %>
Generates paths like this:
/aquarium.1
where I'd typically expect:
/aquariums/1
Right?
Per request... here's what rake routes | grep aquar yields:
(I'm assuming it's that very last route that is messing things up, but I would have thought that it would process these in order. And, just FYI, I originally had that route at the top. Moved it to the bottom assuming it would fix).
aquarium_management_roles GET /aquariums/:aquarium_id/management_roles(.:format) management_roles#index
POST /aquariums/:aquarium_id/management_roles(.:format) management_roles#create
new_aquarium_management_role GET /aquariums/:aquarium_id/management_roles/new(.:format) management_roles#new
edit_aquarium_management_role GET /aquariums/:aquarium_id/management_roles/:id/edit(.:format) management_roles#edit
aquarium_management_role GET /aquariums/:aquarium_id/management_roles/:id(.:format) management_roles#show
PUT /aquariums/:aquarium_id/management_roles/:id(.:format) management_roles#update
DELETE /aquariums/:aquarium_id/management_roles/:id(.:format) management_roles#destroy
aquarium_graphs GET /aquariums/:aquarium_id/graphs(.:format) graphs#index
POST /aquariums/:aquarium_id/graphs(.:format) graphs#create
new_aquarium_graph GET /aquariums/:aquarium_id/graphs/new(.:format) graphs#new
edit_aquarium_graph GET /aquariums/:aquarium_id/graphs/:id/edit(.:format) graphs#edit
aquarium_graph GET /aquariums/:aquarium_id/graphs/:id(.:format) graphs#show
PUT /aquariums/:aquarium_id/graphs/:id(.:format) graphs#update
DELETE /aquariums/:aquarium_id/graphs/:id(.:format) graphs#destroy
aquarium_animals GET /aquariums/:aquarium_id/animals(.:format) animals#index
POST /aquariums/:aquarium_id/animals(.:format) animals#create
new_aquarium_animal GET /aquariums/:aquarium_id/animals/new(.:format) animals#new
edit_aquarium_animal GET /aquariums/:aquarium_id/animals/:id/edit(.:format) animals#edit
aquarium_animal GET /aquariums/:aquarium_id/animals/:id(.:format) animals#show
PUT /aquariums/:aquarium_id/animals/:id(.:format) animals#update
DELETE /aquariums/:aquarium_id/animals/:id(.:format) animals#destroy
aquariums GET /aquariums(.:format) aquariums#index
POST /aquariums(.:format) aquariums#create
new_aquarium GET /aquariums/new(.:format) aquariums#new
edit_aquarium GET /aquariums/:id/edit(.:format) aquariums#edit
aquarium GET /aquariums/:id(.:format) aquariums#show
PUT /aquariums/:id(.:format) aquariums#update
DELETE /aquariums/:id(.:format) aquariums#destroy
aquarium GET /aquarium(.:format) aquariums#show
Thanks in advance!
Greg

Change that last route from this:
get 'aquarium', to: 'aquariums#show', :as => :aquarium
to this:
get 'aquarium', to: 'aquariums#show', :as => :current_aquarium
The problem is that you have to routes named the same thing:
aquarium GET /aquariums/:id(.:format) aquariums#show
aquarium GET /aquarium(.:format) aquariums#show
If you make the change above then that second route will not match when you make those link to calls... as it stands now, the second one is matching and like the route says is using your argument as the :format.
If you do make this change, you may need to tweak some things if you are intentionally linking to 'current_aquarium'.

Related

Rails 3 Routes and Namespaces

I have recently inherited an app where a user's edit account methods along with public account methods all live in the same controller. I am looking to try to make things a little more "Rails-esque" to clean the app up.
Here are the current routes
resources :user, :only => [:update, :show] do
member do
get :edit_account
put :edit_account
put :update_billing_information
put :update_account_type
get :edit_vitals
get :edit_videos
get :edit_athletic
get :edit_academic
get :edit_social
get :edit_news
get :edit_contributors
get :edit_primary_sport_positions
get :edit_primary_sport
get :edit_primary_sport_highlights
delete :remove_alternate_position
get :vitals
get :videos
get :athletic
get :academic
get :social
get :news
post :keep
post :draft
get :share
post :share
get :notes
post :notes
get :contact
post :contact
post :cut
delete :deactivate_account
delete :delete_authorization
end
get :change_current_sport, on: :collection
end
I reckon what I am looking for is, what would be the best rails best practices to move this towards routes that resemble /user/edit/account or /user/edit/billing?

How do you use :as and match a route correctly?

I don't know what I'm doing wrong. This is my route:
resources :stores do
get '/add_shoes' => 'stores#add_shoes', :as => :add_shoes
end
And my path should be: <%= link_to "Add Shoes", add_shoes_path %>
But it gives the error that the path does not exist. How do I use both of them?
If I recall correctly, because it's nested in a resources block, it's going to append stores to the end. So the correct route is add_shoes_stores_path. Sometimes it also adds an index to the end (not sure why), to give you add_shoes_stores_index_path.
Since it's a get call, you could always put it outside the resources block.
Additionally, the more Rails way to do it would be:
resources :stores do
collection do
get :add_shoes
end
end
You've got a bunch of options here on how you want to handle it. But just a quick tip, you can always type rake routes from the command line to get a list of all available routes and where they point.
If you're looking at doing it this way for your clarity of code, just remember this: Your routes should always mention where they're pointing to. That would be both add_shoes and stores. Having an add_shoes_path could point to literally any controller, as it's not really verbose. I would definitely stick to the Rails way of doing it - it will make more sense as you dive in deeper.
Finally, another thought - If you're adding shoes in the stores model, it would make sense for each store to have shoes. You should probably create a new model for Shoes and use RESTFUL routing.

Nested Routing for Single Table Inheritance model rails 3.1

I created a Single table inheritance model in my model file and am having difficulty with the routing. When I use :as in my resource, it renames my named path.
Model file:
class Account < ActiveRecord::Base
belongs_to :user
end
class AdvertiserAccount < Account
end
class PublisherAccount < Account
end
Routes.rb
resources :advertiser_accounts, :as => "accounts" do
resources :campaigns
end
I used :as in my routes because it is a single table inheritance and I want to pass the account_id and not the advertiser_account_id. My link is http://127.0.0.1:3000/advertiser_accounts/1/campaigns
/advertiser_accounts/:account_id/campaigns/:id(.:format)
However, using :as renames my named path from advertiser_account_campaigns to account_campaigns. My route looks like
account_campaigns GET /advertiser_accounts/:account_id/campaigns(.:format) campaigns#index
So when I create a new item using form_for, I would get "undefined method `advertiser_account_campaigns_path'"
Edited: current hacked solution
A hack around way that I am using is to duplicate the code in the routes file. Anyone have suggestions?
resources :advertiser_accounts, :as => "accounts" do
resources :campaigns
end
resources :advertiser_accounts do
resources :campaigns
end
If you run "rake routes" with your setup you'll see this:
account_campaigns GET /advertiser_accounts/:account_id/campaigns(.:format) campaigns#index
POST /advertiser_accounts/:account_id/campaigns(.:format) campaigns#create
new_account_campaign GET /advertiser_accounts/:account_id/campaigns/new(.:format) campaigns#new
edit_account_campaign GET /advertiser_accounts/:account_id/campaigns/:id/edit(.:format) campaigns#edit
account_campaign GET /advertiser_accounts/:account_id/campaigns/:id(.:format) campaigns#show
PUT /advertiser_accounts/:account_id/campaigns/:id(.:format) campaigns#update
DELETE /advertiser_accounts/:account_id/campaigns/:id(.:format) campaigns#destroy
accounts GET /advertiser_accounts(.:format) advertiser_accounts#index
POST /advertiser_accounts(.:format) advertiser_accounts#create
new_account GET /advertiser_accounts/new(.:format) advertiser_accounts#new
edit_account GET /advertiser_accounts/:id/edit(.:format) advertiser_accounts#edit
account GET /advertiser_accounts/:id(.:format) advertiser_accounts#show
PUT /advertiser_accounts/:id(.:format) advertiser_accounts#update
DELETE /advertiser_accounts/:id(.:format) advertiser_accounts#destroy
So you should use "account_campaingns_path" in this setup, the ":as" actually changes the calls in the code not the paths in the url. If you want to change the paths you should use ":path =>" rather than ":as =>".
The Rails guide on routing also shows some examples with ":as" and ":path" and the resulting paths and helpers, you'll need to search a bit because think they only use in in examples explaining other cases.
Edit: rereading your question, I think you may also want to look at member routes, I'm not sure if that's what you want to mean with it being a single inheritance and not wanting to pass the advertiser_account's ':account_id'?

Questions about rails3 routes

I'm upgrading my app to rails 3, and I am a bit confused about some of the routes. The resourceful ones are easy enough, but how can I set a generic rule for all actions in a specific controller. I tried something like this:
get 'custom/:action/' => {:controller => :custom}
But that didn't work. It seems the new format is "controller#action", but how can I specify the action to be variable?
Also, other than using named routes or resources, is it possible to do shorthand notation to name routes in a specific controller?
i.e. rather than:
get '/tasks', :controller => :home, :action => :tasks, :as => 'tasks_home'
get '/accounts', :controller => :home, :action => :accounts, :as => 'accounts_home'
is it possible to do something a little cleaner, like:
controller => :home do
get :tasks
get :accounts
end
And that would automatically created the named routes?
You can use action as a variable like this:
resource :custom do
match ':action'
end
This will generate
/custom/:action(.:format) customs#:action
custom POST /custom(.:format) customs#create
new_custom GET /custom/new(.:format) customs#new
edit_custom GET /custom/edit(.:format) customs#edit
GET /custom(.:format) customs#show
PUT /custom(.:format) customs#update
DELETE /custom(.:format) customs#destroy
So it will handle your action as a variable URL-s and will add some default CRUD actions as well.
Note that the controller name here is in plural. If you would like to use a route for a controller which name is in singular, use resources instead of resource.
The answer to the second question is almost identical to the first one, use resource:
resource :home do
get :tasks
get :accounts
end
generates:
tasks_home GET /home/tasks(.:format) homes#tasks
accounts_home GET /home/accounts(.:format) homes#accounts
home POST /home(.:format) homes#create
new_home GET /home/new(.:format) homes#new
edit_home GET /home/edit(.:format) homes#edit
GET /home(.:format) homes#show
PUT /home(.:format) homes#update
DELETE /home(.:format) homes#destroy
Note that the matched controller names are in plural again, because of the convention.
Looks like this is related to the persisted field being set to false on nested ActiveResource objects: https://github.com/rails/rails/pull/3107

Rails Routing Error for nested form_for

I now this has been asked a thousand times but that doesn't help me heh :) I've been at this an hour. My form:
= form_for #comment, :url_for => { :action => "create", :controller => "comments"}, :method => :post
my rake routes:
POST /t/:trunk_id/r/:root_id/comments(.:format) {:action=>"create", :controller=>"comments"}
trunk_root_comment GET /t/:trunk_id/r/:root_id/comments/:id(.:format) {:action=>"show", :controller=>"comments"}
The error:
undefined method `comments_path' for #<#<Class:0x007fed2c713128>:0x007fed2c71cc78>
If I name space the form to:
= form_for [:trunk_root, #comment], :url_for => { :action => "create", :controller => "comments"}, :method => :post do |f|
which should make the route trunk_root_comments_path.. which is correct according to the rake routes.. I get:
No route matches {:controller=>"comments", :format=>nil}
Help is very much appreciated.. been looking at this for hours..
UPDATE:
Thank you Ryan for such a great answer! A very clear explanation of something I was just sort of 'throwing things' at, now at least I understand better. I actually already had 'trunk_root_comments_path' available in my rake routes, and I had tried a couple of the combinations you mentioned, but I wasn't really grocking what I was missing, so you helped. I'm using Mongo and I don't actually have a Trunk model, I just have an attribute on roots called #root.trunk, though I have a trunk controller and therefore its a part of my routes(maybe a bad idea idk).
So I tried your TLDR and it said error:
Undefined method 'root_comments_path'
.. cause no Trunk model exists, I assume?.. so I made #trunk just equal the correct id with
= form_for [#trunk, #root, #comment] do |f|
<- and I got 'undefined method `politics_root_comments_path''.. I figured well.. that probably makes sense.. since I'm failing I must as well try your most explicit version:
= form_for #comment, :url => (trunk_root_comments_path(:trunk_id => #root.trunk, :root_id => #root.id)) do |f|
and sure enough that worked... so I'm not quite sure how to do it shorter then this.. the odd thing for me is I have another nested resource "photos" at the same level of depth in the routes and I was able to get that to work with = form_for [:trunk_root, #photo], :html => { :class => 'root_form' } do |f|.. but here for some reason I couldn't.. anyways I'd say you gave me enough to understand 100% but I think I went from 20% understanding to 50% understanding.. I know now that id's ARE important to routes, and the named helpers need access to them. I got an introduction to how the url_helper works, but would need to read more on it to really grock it fully I think. I'm also now able to construct proper routes in their longer form at least to get through tricky situations like this. So thank you :)
TL;DR You need to specify both a :trunk_id and a root_id in your URL or use form_for like this:
<%= form_for [#trunk, #root, #comment] do |f| %>
Rails is attempting to build a URL from the hash you're giving it, but that hash doesn't match anything in its routing table. You could do this:
{ :controller => "comments", :action => "create", :trunk_id => trunk.id, :root_id => root.id }
But that's really a bit tl;dr.
The cooler way to do it is this:
trunk_root_comments_path(trunk, root)
Where trunk and root are Trunk and Root instances respectively.
Now, if you want to be super-wicked-cool, do it like this:
<%= form_for [trunk, root, comment] do |f| %>
Science!
So how does this work? Elementary, my dear:
Rails first recognises that we're using form_for using an Array and that we mean business. Rails uses this array passed in and builds a URL out of it. It does this by using the routing helpers that are defined by the routes. Unfortunately, you've defined your routes in a funny way that don't play nice with this, but don't fear! We can fix this.
The way you can do it is this where you have this in config/routes.rb:
post '/t/:trunk_id/r/:root_id/comments'
Instead put this:
post '/t/:trunk_id/r/:root_id/comments', :as => "trunk_root_comments"
You may alternatively already have this:
match '/t/:trunk_id/r/:root_id/comments', :via => :post
Which should become this:
match '/t/:trunk_id/r/:root_id/comments', :via => :post, :as => "trunk_root_comments"
Either way, you've now got not one, but two(!!) path helpers defined by the routes. These aretrunk_root_comments_path and trunk_root_comments_url respectively. The names of these methods are super important for what I am about to explain to you. Pay attention!
So, back to our little form_for call:
<%= form_for [trunk, root, comment] do |f| %>
Rails knows that we're using an Array because it can see it. What it does with this Array may seem like magic, but isn't really.
Rails will take each element of this array and build a routing helper method name up from the different parts. This isn't actually part of form_for, but another method called url_for that you can use by itself:
url_for([trunk, root, comment])
In the beginning, this routing helper method name generated by url_for is simply an empty array ([]). Nothing special at all.
But then what happens is special!
The first element is going to be a persisted instance of the Trunk class. By "persisted" I mean that it's an object that maps directly to a record in the database. Yay ORMs!
Rails will know this, and so will turn the routing helper into this: [:trunk].
The second element is going to be a persisted instance of the Root class. Rails also knows this (damn, Rails is smart!) and will then append this to the array, turning it into [:trunk, :root]. Awesome.
The third (and final) element is then checked by Rails. It sees that (in this case) it's a non-persisted element, i.e. it's not been saved to the database.. yet. Rails treats this differently and will instead append [:comments] to the array, turning it into this:
[:trunk, :root, :comments]
See where I'm going with this now?
Now that Rails has done it's thing (or thang, if you like) it will join these three parts together like this: trunk_root_comments, and just for good measure it'll put _path on the end of it, turning it into the final trunk_root_comments_path helper.
And then! Man, and then... Rails calls this method and passes it arguments! Just like this:
trunk_root_comments_path(:trunk_id => trunk.id, :root_id => root_id)
This generates a full path to the resource like this:
/t/:trunk_id/r/:root_id/comments
And bam! Full circle! That's how Rails will know to generate the URL and you don't have to use ugly hashes anymore.
Success!
Not sure if you have this route set up but try:
= form_for #comment, :url => trunk_root_comments_path