Conditional layout - show

Hello all,
I’m learning Ruby on Rails and still kind of new at this.

I have two different layouts (standard page and dialog box) for a view
(show).
How do I set each layout in the controller to show the different layout?

Something like this…

  • Controller -
    def show
    @project = Project.find(params[:id])

if ‘view is page’
render :layout => ‘page_layout’
else
render :layout => ‘dialog’

end

Where would I set the variable? in the show.html.erb?

Thank you for any help.

JohnM

John M. wrote:

I have two different layouts (standard page and dialog box) for a view
(show).
How do I set each layout in the controller to show the different layout?

I don’t know exactly what you’re trying to accomplish from the
information you provided, but this approach doesn’t “feel” right to me.

I would put the content of the page/dialog into a partial. I’m going to
assume that your dialog version will be rendered by an AJAX call.

In that case I would use respond_to to determine which layout to render:

respond_to do |format|
format.html # Render page with default layout
format.js # Render the dialog version
end

In case you’re not rendering the “dialog” version with AJAX then you
might want to use a custom MIME type to render the alternate layout:

respond_to do |format|
format.html # Render page with default layout
format.dlog # Render the dialog version as HTML using custom format
type
end

Where would I set the variable? in the show.html.erb?

I’m not sure how you will be determining which layout to use, but if you
absolutely had to store this in a variable you would need an instance
variable on your controller.

However, as I mentioned before moving the decision into the request
seems like the better approach.

Robert W. wrote:

I would put the content of the page/dialog into a partial. I’m going to
assume that your dialog version will be rendered by an AJAX call.

On second thought the partial may not be necessary.

2009/10/14 John M. [email protected]:

Hello all,
I’m learning Ruby on Rails and still kind of new at this.

I have two different layouts (standard page and dialog box) for a view
(show).

It is inadvisable to use the word layout here as that word has a
particular meaning in Rails. I take it that you mean that you have
two different ways that you want to show the same data.

 else
  render :layout => ‘dialog’

end

Where would I set the variable? in the show.html.erb?

What is not clear is how you know which view you want to show. Does
the user do something to indicate which view he wants or is it
something else? Is it possibly that you want a different view when
just displaying the data from when the user is entering the data in
the first place (or editting it)?

Colin

I apologize to all for being unclear. It’s just the newbie coming out.
Let’s try again.

I have a ‘projects’ layout within the ‘Views’ folder, with a heading,
nav bar, and content area.
When you select “show” on the index.html.erb of the ‘projects’ View
folder, the project information displays in the content area. I wish to
not change this.

Within another template, I have a link that produces a dialog box
(Prototype Window) that displays the project information. When the
dialog box appears, the project information displays as the first
layout, with header, nav bar and content area.

I created a layout for displaying just the content area and set it in
the controller.

  • controller -
    def show
    @project = Project.find(params[:id])

    render :layout => ‘dialog’

end

When I go back to the original index.html.erb and click on the ‘show’
link, my 'header/nav bar/content area layout disappears. All that
displays is the project information without any styling.

I hope this clears up things.

JohnM

Colin L. wrote:

2009/10/14 John M. [email protected]:

Hello all,
I’m learning Ruby on Rails and still kind of new at this.

I have two different layouts (standard page and dialog box) for a view
(show).

It is inadvisable to use the word layout here as that word has a
particular meaning in Rails. I take it that you mean that you have
two different ways that you want to show the same data.

 else
  render :layout => ‘dialog’

end

Where would I set the variable? in the show.html.erb?

What is not clear is how you know which view you want to show. Does
the user do something to indicate which view he wants or is it
something else? Is it possibly that you want a different view when
just displaying the data from when the user is entering the data in
the first place (or editting it)?

Colin

2009/10/14 John M. [email protected]:

I apologize to all for being unclear. It’s just the newbie coming out.
Let’s try again.

I have a ‘projects’ layout within the ‘Views’ folder, with a heading,
nav bar, and content area.

Is that literally a layout, ie views/layouts/projects.html.erb?

When you select “show” on the index.html.erb of the ‘projects’ View
folder, the project information displays in the content area. I wish to
not change this.

So that is views/projects/show.html.erb using
layouts/projects.html.erb as the layout?

Within another template, I have a link that produces a dialog box
(Prototype Window) that displays the project information. Â When the
dialog box appears, the project information displays as the first
layout, with header, nav bar and content area.

Lost me I am afraid, what is a template? When you say you have a link
that produces a dialog box is that a javascript box called up without
going back to the server or what?

I created a layout for displaying just the content area and set it in
the controller.

Is this the projects controller or another one? We have already
decided that projects/show is working ok.

link, my 'header/nav bar/content area layout disappears. Â All that
displays is the project information without any styling.

It appears that you are saying that clicking on the same link (which
presumably is just a get on the controller, nothing fancy, shows a
different result in the browser? Odd to say the least. You have not
shown us the code for projects_controller#show. Is there some logic
in there that could make it do something different the second time?
Have a look at the generated html (View, Page source or similar in
browser) and see what the difference is. Have a look in
development.log and see what is different there. That may give you a
clue.

I hope this clears up things.

Not entirely. :slight_smile:

Colin