2017-03-16 118 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>"