2017-03-16 117 views
1

在我的控制器中,我将一个变量设置为一个HTML字符串,以便说明基本情况。但是,#注释了其余部分。我怎么能逃避这个?Rails#在HTML href中注释掉行

@chart_data = "<p id='chart-notice'>If you <a href="#SOME_ID">login to your social media apps</a>, this section will display social analytics.</p>" 

注:这不是一个.html.erb文件是.rb

回答

3
@chart_data = <<-HTML.strip! 
    <p id='chart-notice'>If you <a href="#SOME_ID">login to your social media apps</a>, this section will display social analytics.</p> 
HTML 

或者

@chart_data = %Q{ 
    <p id='chart-notice'>If you <a href="#SOME_ID">login to your social media apps</a>, this section will display social analytics.</p> 
}.strip! 

或者

@chart_data = "<p id='chart-notice'>If you <a href=\"#SOME_ID\">login to your social media apps</a>, this section will display social analytics.</p>" 

,请注意\

+0

如果你使用两种类型的引号,'%Q'是最混乱的解决方案。 '<<'是HEREDOC风格,对于多行字符串也很适用。 – tadman

0

尝试下面的代码:

@chart_data = "<p id='chart-notice'>If you <a href='#SOME_ID'>login to your social media apps</a>, this section will display social analytics.</p>"