Singular route serving parameter :object_id instead of :id - ruby-on-rails-3

Say I have an object called invoice. In routes.rb I have
resources :invoices do
get "pay"
end
When I run rake routes, the route is generated as
invoice_pay GET /invoices/:invoice_id/pay(.:format) invoices#pay
and the parameter is :invoices_id instead of :id
If I use a match statement:
match "invoices/:id/pay" => "invoices#pay", :via => :get
I get:
GET /invoices/:id/pay(.:format) invoices#pay
It seems to me that the route should be pay_invoice_path(#invoice), however, I have not found suitable documentation on this. Any suggestions?

i think what you are trying to do is
resources :invoices do
get "pay", :on => :member
end
have a look at the guides: http://guides.rubyonrails.org/routing.html

Related

rails adding "static_content" on routes

I have routes that work perfectly on one machine, but on another machine they are failing and I've had a hard time to figure out what is wrong. On the failing machine it return the following errors for get /groups/my and groups/ respectively
No route matches {:controller=>"groups/owner/static_content", :topic=>"general"}
No route matches {:controller=>"groups/static_content", :topic=>"general"}
I have no idea where
static_controller
and
:topic=>"general"
come from since they don't appear anywhere in my routes file. Basically I have a route like
namespace :groups , :as => nil do
root :to => 'groups#index'
resources :groups, :only => [:show, :new, :create], :path => '' do
collection do
get :search
get 'my' => 'owner/groups#my', :as => :my
end
member do
post :subscribe
end
... other resources within a group
end
end
Any idea what I have done wrong or I'm missing? I'm using rails 3.2.2 and ruby 1.9.3 on rvm
A route is usually called from the views, so always check your view for action_controller_name_path if Controller::Action can not be found!

rails routes optional parameters types by :id or :name

Scenario:
I would like to have a url path where you could look someone up by an :id or :name:.
For user/5 or /user/tom would all point to the same user
routes.rb
controller 'user' do
get 'user/:id'
get 'user/:name'
end
test/routes/user_routes.rb
test "/user/:id" do
assert_routing "/user/5", :controller => "user", :action => "find_by_id", :id=>"5"
end
test "/user/:name" do
assert_routing "/user/tom", :controller => "user", :action => "find_by_name", :name=>"tom"
end
I am not exactly sure if this is the right design decision with URL paths.
Looking for guidance
I don't think what you are doing is going to work, because the routes you gave are ambiguous, because rails routes can't say that url like user/15 has name or id.
Of course there is a way to do this, by specifying regular expression. Since id will always be a number, we can have regular expression check for it
controller 'user' do
get 'user/:id', :id => /\d+/
get 'user/:name', :name => /[A-Za-z][\w\s]+/
end
The above statements put a constraint of regular expression. In your controller you can check like
if params[:id]
# Get by id
else
# Get by name
You can also do this by passsing get parameters and handling it in controller in the same way.
Thanks

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

Pagination in Rails 3 with Kaminari always "destroys" one part of the URL

I'm using a Rails 3 App with Kaminari for Pagination. Because of my language, SEO and friendly URLs I want to change my URLs looks like. But it seems I have to decide between friendly pagination links and or displaying the correct method names. But first things first:
I have a model, which is called "pages" and contains some pages which can't be applied to only one model or don't contain any model. The following is a snippet of my "routes.rb":
match'/neugikeiten', :to => 'pages#neuigkeiten'
scope(:path_names => { :new => "neu", :edit => "bearbeiten", :delete => "loeschen", :index => "index", :page => "seite" }) do
resources :news, :path => "neuigkeiten"
end
As you can see, I match every page from pages to a single name and match all my other models and methods (I only used one as example) with scope. All of this works just fine. But now I have to add the Kaminare routes, for which the route looks like this:
resources :pages do
get 'seite/:page', :action => :neuigkeiten, :on => :collection
end
So, basically what I want to get is an URL like this:
/neuigkeiten/seite/2
The 2 is just an example and seite is the german word for page. But what i do get, is this:
/pages/seite/2
So, I get the model name, instead of the name I defined for this single page. I already tried switching the different codes but either I just have the original model name in the URL or I don't get nice URLs for pagination, which look like this:
/neuigkeiten?page=2
And if I try to match the pages without the resource like this:
match'/tagebuch/seite/:page', :to => 'pages#tagebuch', :on => :collection
I get the following error.
can't use collection outside resources scope
It seems to me that there should be another method to do this, because I can't be the only one having this kind of problem.
I'm glad for any help!
If anybody is interested in an solution, I found one myself. It's not perfect, but it works.
I had to install an extra gem for the localisation called "i18n_routing". Then in my routes.rb I did:
localized do
resources :news, :path => "neuigkeiten" do
get 'seite/:page', :action => :index, :on => :collection
end
end
The rest is done in the localized language file (en.yml).

Naming params of nested routes

resources :leagues do
resources :schedule
end
This generates:
leagues/:id
leagues/:league_id/schedule/:id
How can I keep the league ID from changing param names?
So it'll be:
leagues/:id
leagues/:id/schedule/:schedule_id
No, please do not do this.
The reason for it being this way is that it provides a common interface for nested resources across every single application. By making it different in your application, you're effectively going "against the grain" of Rails. Rails has a strict set of conventions that you should stick to. When you stray from this path, things get messy.
However, if you do want to shoot yourself in the foot, metaphorically speaking, you will need to define the routes manually. Here's the routes for the seven standard actions in a controller:
get 'leagues/:id/schedules', :to => "schedules#index", :as => "league_schedules"
get 'leagues/:id/schedule/:schedule_id', :to => "schedules#show", :as => "league_schedule"
get 'leagues/:id/schedules/new', :to => "schedules#new", :as => "new_league_schedule"
post 'leagues/:id/schedules', :to => "schedules#create"
get 'leagues/:id/schedule/:schedule_id/edit', :to => "schedules#edit", :as => "ed it_league_schedule"
put 'leagues/:id/schedule/:schedule_id', :to => "schedules#update"
delete 'leagues/:id/schedule/:schedule_id', :to => "schedules#destroy"
As you can see, it's quite ugly. But, if you really really really want to do it this way, that's how you'd do it.
You can set "param" option on resource route to override the default "id" param:
resources :leagues do
resources :schedule, param: schedule_id
end
refs to the Rails Routing Doc: http://guides.rubyonrails.org/routing.html#overriding-named-route-parameters
It appends the ID to the nested_param which is a bummer because I would like mine to be without the singular name. It looks like they really don't want you to make it only like :id as it could have conflicts. Plus it would be a bit of a diff from the normal restful routing that rails likes to use.
https://github.com/rails/rails/blob/5368f2508651c92fbae40cd679afbafdd7e98e77/actionpack/lib/action_dispatch/routing/mapper.rb#L1207
namespace :account, defaults: { type: 'account' }do
resources :auth, param: :lies_id, only: [] do
get :google
end
end
Rake routes returns the following
$ rake routes | grep /account/auth
account_auth_google GET /account/auth/:auth_lies_id/google(.:format)
So the solution which seams simpler is to just change the controller to use the nested param name it creates.