Ruby on Rails seems to be auto-escaping html created by link_to

后端 未结 4 696
遥遥无期
遥遥无期 2020-12-20 20:42

Here is my code, I\'m trying to display a list of links to a bboy\'s crews in sentence form with .to_sentence


    <% if         


        
相关标签:
4条回答
  • 2020-12-20 21:21

    Rails 3 now automatically escapes everything, in order to output raw HTML use this:

    <%= some_string.html_safe %>
    

    or this:

    <%= raw @some_html_string %>
    

    Thanks to macek for a hint.

    For additional details: http://markconnell.co.uk/posts/2010/02/rails-3-html-escaping

    0 讨论(0)
  • 2020-12-20 21:24

    You can (and should) you the raw method

    <%= raw @some_html_string %>
    
    0 讨论(0)
  • 2020-12-20 21:24

    I recommend you move this block of code to an helper and then use the .html_safe method to obtain the expected results.

    0 讨论(0)
  • 2020-12-20 21:33

    I agree with Kleber S, you should move this into a helper, because that's a lot of logic for a view

    def crews_description(crews)
      if crews.empty?
        content_tag('em', 'Independent')
      else
        label = "Crew"
        label = label.pluralize if crews.size > 1
        crews_links = crews.map {|crew| link_to(h(crew.name), crew)}.to_sentence
        content_tag('span', label) + crews_links.html_safe
      end
    end
    

    and in your view:

    <span class="affiliation">
      <%= crews_description(@bboy.crews)
    </span>
    
    0 讨论(0)
提交回复
热议问题