form_for with module and namespace

I have the follow models in Finance module:

class Finance::BillRec < ActiveRecord::Base   ...   has_many :bill_rec_offs, :dependent => :destroy   ... end

class Finance::BillRecOff < ActiveRecord::Base   ...   belongs_to :bill_rec   ... end

I'm doing this on my form_for:

<%= form_for([@bill_rec, @bill_rec_off]) do |f| %>   ... <% end %>

routes.rb

namespace :finance do   resources :bill_recs do     resources :bill_rec_offs   end end

And the error:

undefined method `finance_bill_rec_finance_bill_rec_offs_path' for #<#<Class:0x000000070757e0>:0x0000000708bec8>

However, the route finance_bill_rec_bill_rec_off_path(@bill_rec_off) works well.

How can I do on a form_for with namespace and nested routes with module?

did you check with "rake routes"?

rake routes:

finance_bill_rec_bill_rec_offs GET /finance/bill_recs/:bill_rec_id/bill_rec_offs(.:format) finance/bill_rec_offs#index                                    POST /finance/bill_recs/:bill_rec_id/bill_rec_offs(.:format) finance/bill_rec_offs#create new_finance_bill_rec_bill_rec_off GET /finance/bill_recs/:bill_rec_id/bill_rec_offs/new(.:format) finance/bill_rec_offs#new edit_finance_bill_rec_bill_rec_off GET /finance/bill_recs/:bill_rec_id/bill_rec_offs/:id/edit(.:format) finance/bill_rec_offs#edit      finance_bill_rec_bill_rec_off GET /finance/bill_recs/:bill_rec_id/bill_rec_offs/:id(.:format) finance/bill_rec_offs#show                                    PUT /finance/bill_recs/:bill_rec_id/bill_rec_offs/:id(.:format) finance/bill_rec_offs#update                                    DELETE /finance/bill_recs/:bill_rec_id/bill_rec_offs/:id(.:format) finance/bill_rec_offs#destroy

The routes works well, like <%= link_to "new", new_finance_bill_rec_bill_rec_off(@bill_rec) %>

This is because your models are included in other class Finance, and it is very possible for developer to make a mistake understanding of paths or whatever stuff which uses .model_name of your classes.

In your case, the concern is - route:

namespace :finance do resources :bill_recs do    resources :bill_rec_off end end

generated 'finance_bill_rec_bill_rec_off_path()' - thats right in 'namespace' logic.

'finance' part in this route is just because you pass the argument 'namespace' and not because your model_name is 'finance_bill_rec'. But the <%= form_for([@bill_rec, @bill_rec_off]) do |f| %> uses .polymorphic_path() method, and returns you 'finance_bill_rec_finance_bill_rec_offs_path'. Twice 'finance' in that case is because .model_name returns 'finance_bill_rec' and 'finance_bill_rec_off' - thats the clear and real path name you should implement.

Yeah, we'd better always let Rails operate on their own methods, hidden from us, like .model_name() or .polymorphic_path().

Ok, working with route: 'namespace :finance do ..' - is this really what you looking for? I think you just want to operate with your models under browser's string '/finance/...', right? Try another approach for that:

resources :bill_recs, :path => '/finance' do resources :bill_rec_off end

OR

scope "/finance" do resources :bill_recs do    resources :bill_rec_off end end

Anyway, you should implement 'finance_bill_rec_finance_bill_rec_offs_path' without 'namespace'

With the resources :bill_recs, :path => '/finance' ... and scope "/finance" do ... it appears a message:

ActionController::RoutingError (uninitialized constant BillRecOffController):

I need to put the models into a namespace beacause there are many files in my

project, then Finance::BillRec and Finance::BillRecOff are on Finance model. And also on controllers.

And BillRecOff belongs to BillRec, So, it has be as a resource of BillRec resources :bill_recs do    resources :bill_rec_off end

How I can do?

Issue resolved: