Context sensitive edit/new action

I have the following associations:

has_one :client_role

belongs_to :entity

There should be two ways to add a client role, one where the entity
exists and one where the entity does not.

In the case where the entity does not exist I have wired the client
controller to initialize an entity and an entity.client, populate both
from client/new, and add both via client/create. This works.

Here is my problem.

The client/edit method can logically be reached either from
clients/index (client role exists) or from entities/index (client role
may or may not exist). However, the :id passed via params in each case
refers to a different model, client and entity respectively. Ideally,
when calling from entities/index where no client role exists I should
call new_client_path. However, new in this sense implies an existing
entity, something that I cannot pass via new_client_path and which would
not be understood by as written in any case.

Basically, what I must do is detect what model the :id passed belongs
too in edit and set up the necessary objects dependent upon that
determination. Or, I have to call a method so that this determination
is already satisfied. I cannot simply find against both models inside
the controller and see which one fails since it is possible that the
edit call is for an existing client role. Further, when a new client
role is required the entity id: passed might refer to an entirely
different client role which then would be returned in error.

Is this a case where I need a separate controller and special entry in
the routes file to handle this need?

map.resources :clients

map.resources :entities do |entity|
entity.resources :client, :controller => ?, :method =>

Thanks in advance for any assistance offered.

James B. wrote:

I have the following associations:

has_one :client_role

belongs_to :entity

I have this in my config/routes

map.resources :clients do |client|
# A client role has only one entity.
client.resource :entity

map.resources :entities do |entity|
# An entity can only have one client role
entity.resource :client
entity.resources :locations
# And only one vendor role
entity.resource :vendor

And rake routes gives me this:

new_entity_client GET /entities/:entity_id/client/new
{:action=>“new”, :controller=>“clients”}
formatted_new_entity_client GET
/entities/:entity_id/client/new.:format {:action=>“new”,
edit_entity_client GET /entities/:entity_id/client/edit
{:action=>“edit”, :controller=>“clients”}
formatted_edit_entity_client GET
/entities/:entity_id/client/edit.:format {:action=>“edit”,
entity_client GET /entities/:entity_id/client
{:action=>“show”, :controller=>“clients”}
formatted_entity_client GET
/entities/:entity_id/client.:format {:action=>“show”,
PUT /entities/:entity_id/client
{:action=>“update”, :controller=>“clients”}
/entities/:entity_id/client.:format {:action=>“update”,
DELETE /entities/:entity_id/client
{:action=>“destroy”, :controller=>“clients”}
/entities/:entity_id/client.:format {:action=>“destroy”,

But if I write this in views/entities/index

33 <%- if entity.client.nil? -%>
34 <%= link_to ‘Add Client Role’, new_entity_client(entity)

Then I see this:

NoMethodError in Entities#index

Showing entities/index.html.erb where line #34 raised:

undefined method `new_entity_client’ for #ActionView::Base:0xb793f9d8

_run_erb_47app47views47entities47index46html46erb' app/views/entities/index.html.erb:11:ineach’
_run_erb_47app47views47entities47index46html46erb' app/controllers/entities_controller.rb:13:inindex’

and params contains nil.

I am somewhat confused at this point. Rakes routes shows that
new_entity_client is defined and Rails says that it is not. Can anyone
point me in the right direction?