Lance Pollard
Lance Pollard

Reputation: 79228

Check if a layout exists in Rails?

Is there a standard way to check if a view layout exists from within a Controller in Rails? I'm trying to allow the user to determine the layout, but it needs to exist first.

Upvotes: 7

Views: 4198

Answers (3)

Joni
Joni

Reputation: 3357

You can use template_exists? which is an alias for exists?

For example: template_exists?("layout_name", "layouts")

Upvotes: 25

Rishav Rastogi
Rishav Rastogi

Reputation: 15492

I think the better way would be store these values in a database, let a user choose from that.

Instead of trying to verify through what has been suggested in the previous post.

Use a Layout Model and let the user choose.

Upvotes: 0

Daniel Beardsley
Daniel Beardsley

Reputation: 20367

There is no standard public way as far as I know. You could use a rudimentary call like this:

layouts = Dir['app/views/layouts/*'].map {|f|
  File.basename(f, '.html.erb')  # returns 'layout' for 'layout.html.erb'
}

Upvotes: 3

Related Questions