# Context sensitive edit/new action

**URL:** <https://www.ruby-forum.com/t/context-sensitive-edit-new-action/133559>\
**Category:** Rails\
**Created:** [April 1, 2008, 5:14pm UTC](https://www.ruby-forum.com/t/context-sensitive-edit-new-action/133559 "2008-04-01T17:14:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [April 1, 2008, 5:14pm UTC](https://www.ruby-forum.com/t/context-sensitive-edit-new-action/133559/1 "2008-04-01T17:14:29Z")

</div>

I have the following associations:

Entity  
has\_one :client\_role

ClientRole  
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 clients\_controller.new 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 =\>  
edit/new?  
end

Thanks in advance for any assistance offered.

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [April 1, 2008, 10:34pm UTC](https://www.ruby-forum.com/t/context-sensitive-edit-new-action/133559/2 "2008-04-01T22:34:59Z")

</div>

James B. wrote:

> I have the following associations:
> 
> Entity  
> has\_one :client\_role
> 
> ClientRole  
> 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  
end

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  
end

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”,  
:controller=\>“clients”}  
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”,  
:controller=\>“clients”}  
entity\_client GET /entities/:entity\_id/client  
{:action=\>“show”, :controller=\>“clients”}  
formatted\_entity\_client GET  
/entities/:entity\_id/client.:format {:action=\>“show”,  
:controller=\>“clients”}  
PUT /entities/:entity\_id/client  
{:action=\>“update”, :controller=\>“clients”}  
PUT  
/entities/:entity\_id/client.:format {:action=\>“update”,  
:controller=\>“clients”}  
DELETE /entities/:entity\_id/client  
{:action=\>“destroy”, :controller=\>“clients”}  
DELETE  
/entities/:entity\_id/client.:format {:action=\>“destroy”,  
:controller=\>“clients”}

But if I write this in views/entities/index  
…  
33 \<%- if entity.client.nil? -%\>  
34 \<%= link\_to ‘Add Client Role’, new\_entity\_client(entity)  
-%\>  
36

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

app/views/entities/index.html.erb:34:in  
`_run_erb_47app47views47entities47index46html46erb' app/views/entities/index.html.erb:11:in`each’  
app/views/entities/index.html.erb:11:in  
`_run_erb_47app47views47entities47index46html46erb' app/controllers/entities_controller.rb:13:in`index’

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?
