How to get the right template

Back to my notorious entity : client relationship.

Entity
has_one :client

Client
belongs_to :entity.

The route:

map.resources :entities do |entity|
# An entity can only have one client role
entity.resource :client, :controller => ‘entity_client’
# An entity can have many locations
entity.resources :locations
# And only one vendor role
entity.resource :vendor
end

Given

the URL = http://localhost:3000/entities/3/client

Naturally enough the default respond_to call to show in
entity_clients_controller.rb expects to find it in views/entity_clients.
Being perverse, I want to display the show.html.erb template in
views/clients. I tried this code in the controller:

def show
@client = Entity.find(params[:entity_id]).client
respond_to do |format|
format.html { redirect_to ‘/clients/show.html.erb’ }

But this gives me the error:

Routing Error

No route matches “/clients/show.html.erb” with {:method=>:get}.

I tried modifying routes.rb to this:

map.resources :entities do |entity|
# An entity can only have one client role
entity.resource :client,
:controller => ‘entity_client’,
:method => ‘get’,
:path_prefix => ‘clients’

but that gives me this error:

Unknown action

No action responded to 3

So, what is it that do I not understand here?

redirect_to ‘/clients/show.html.erb’

redirect to a html.erb file?
you should redirect to an action/controller

or use render :template => ‘/clients/show.html.erb’

Thorsten M. wrote:

redirect_to ‘/clients/show.html.erb’

redirect to a html.erb file?
you should redirect to an action/controller

or use render :template => ‘/clients/show.html.erb’

Thanks, that is exactly what I ended up doing.

def show
@client = Entity.find(params[:entity_id]).client

respond_to do |format|
format.html { render :template => ‘clients/show’ }

If you just want to show it, use render :template, don’t use
redirect_to.

redirect_to will use the routes again, therefore you would need to
specify the controller and action (ie you should get url_for to render
it for you).

Julian.

Learn Ruby on Rails! Check out the FREE VIDS (for a limited time)
VIDEO #3 out NOW!
http://sensei.zenunit.com/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs