0

So I'm writing a helper to help me fill out the carousel of bootstrap for Rails. This is the piece of code of the helper method:

def carousel_item(element, active = false)          
    content_tag :div, class:"item"+(active ? " active" : "") do
        image_tag("carousel/"+element.name) +
        if !element.desc.nil? || !element.title.nil? || !element.link.nil? do
            content_tag(:div, {class:'container'}) do
                !element.title.nil? ? content_tag(:h1, element.title) +
                !element.desc.nil? ? content_tag(:p, element.desc, class:"lead") +
                !element.link.nil? ? link_to(element.linktitle, url_for(action: element.link), {class:"btn btn-large btn-primary"}) +
            end             
        end
    end     
end

But then im getting this error:

app/helpers/application_helper.rb:22: syntax error, unexpected keyword_end, expecting ':'

pointing after the last end prior closing the method.. hours fighting against this thing, still can't find the solution.

Andres Perez
  • 149
  • 15

2 Answers2

0

...

!element.title.nil? ? content_tag(:h1, element.title) +
(!element.desc.nil? ? content_tag(:p, element.desc, class:"lead") +
(!element.link.nil? ? link_to(element.linktitle, url_for(action:   element.link), {class:"btn btn-large btn-primary"}) : nil ) : nil) : nil
Valery Kvon
  • 4,438
  • 1
  • 20
  • 15
0

First i fixed my inline conditional writing the : "" at the end of each condition. (Thanks Valery Kvon for that)then and finally I found the solution to my nesting problem with the .html_safe method, i just stored everything on a string var and then return it with html_safe. Here is the code:

def carousel_item(element, active=false )
    if !element.nil?
        content_tag(:div,class:"item"+(active ? " active" : "")) do 
            image_tag("carousel/"+element.name) + #important to concat with next part
            if !element.desc.nil? || !element.title.nil? || !element.link.nil?
                content_tag(:div,class:"container") do
                    content_tag(:div,class:"carousel-caption") do
                        c = ""
                        !element.title.nil? ?   c += content_tag(:h1,element.title) : ""
                        !element.desc.nil? ?    c += content_tag(:p,element.desc,class:"lead") : ""
                        !element.link.nil? ? c += link_to(element.linktitle,url_for(action: element.link),class:"btn btn-large btn-primary") : ""
                        c.html_safe #this is the key that solves eveything
                    end
                end
            else
                nil
            end
        end
    end
end

Here some others examples of how to do it html_safe and helpers in rails 3

Andres Perez
  • 149
  • 15