Given:
class Entity
has_one :client
class Client
belongs_to :entity
I have the situation where a client role either can be created
coincidentally with an entity or subsequent to an entity’s creation. I
have therefore created two controllers to handle this within the
constraints of ReST: clients_controller, which handles the coincidental
creation situation; and entity_clients_controller, which handles the
case of adding a client role to an existing entity.
Now it seems desirable that I use the same view to do this since the
user input fields are exactly the same in both cases. The only
difference being is that in the latter case the entity values are
already provided. So, inside
entity_clients_controller I have this code:
def new
@entity = Entity.find(params[:entity_id])
@client = @entity.build_client
@client.effective_from = Time.now.to_date
respond_to do |format|
format.html { render :template => ‘clients/new’ } # new.html.erb
format.xml { render :xml => @client }
end
end
Which displays the contents of the passed entity in the fields of the
new template. Everything works up to this point. My problem is that
the submit button on the clients/new view redirects back to
clients_controller or course. However, clients_controller expects to
create a new entity as well as a client and that simply will not do.
My question is therefore, how do I handle this? I want a single view,
called from two controllers, to return to the calling controller. How is
this properly done in Rails? Do I really need a separate view?