Question

  • ruby 1.9.2p290
  • rails 3.1.1

How to create a html element from controller in SLIM templates?

I will explain:

In my views, I want to change the "h1" html tag by some conditions.

But I want to put the logic inside controller.

case params[:controller] when "recipes", "chefs"
    case params[:action] when "show", "index"
      @h_number = "h2"
    else
      @h_number ="h1"
    end
  else
    @h_number ="h1"
end

In my SLIM view, I would like something like that:

= @h_number#logo
    = link_to image_tag("image.png"), root_path

Which results in:

<h1 id="logo"><a href="/"><img src="image.png"></a></h2>

or

<h2 id="logo"><a href="/"><img src="image.png"></a></h2>

Is it possible?

Am I clear? Sorry about my english.

Was it helpful?

Solution

I highly doubt this is possible. But you could always create a helper method to do this

In your application helper file,

module ApplicationHelper
    def logo
        num = case params[:controller] when "recipes", "chefs"
          case params[:action] when "show", "index" then 2 end
        end || 1

        "<h#{num}>" + link_to(image_tag("image.png"), root_path) + "</h#{num}>"
    end
end

In your template

body
  header
    == logo

And your controllers don't need anything.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top