我有以下代码,我的问题是当我尝试添加或其中的任何其他html标记时,如图所示它不起作用,它显示为普通字符串。
<%= link_to image_tag(class_door(student_class), onMouseover: "this.src='/assets/open_door.png';", onMouseout: "this.src='/assets/closed_door.png'" ),'javascript:;' ,class:'popovers','data-content'=>"Number of Students: #{student_class.students.count}<br><button>Click to enter</button>" ,'data-original-title'=>"#{student_class.name.capitalize}" %>
我也试过添加html_safe就像这样
<%= link_to image_tag(class_door(student_class), onMouseover: "this.src='/assets/open_door.png';", onMouseout: "this.src='/assets/closed_door.png'" ),'javascript:;' ,class:'popovers','data-content'=>"Number of Students: #{student_class.students.count}<br><button>Click to enter</button>".html_safe ,'data-original-title'=>"#{student_class.name.capitalize}" %>
但这也不起作用并给出相同的结果
答案 0 :(得分:1)
尝试添加'data-html'=>true
。你想要的东西是:
<%= link_to image_tag(class_door(student_class), onMouseover: "this.src='/assets/open_door.png';", onMouseout: "this.src='/assets/closed_door.png'" ),'javascript:;' ,class:'popovers', 'data-html'=>true, 'data-content'=>"Number of Students: #{student_class.students.count}<br><button>Click to enter</button>" ,'data-original-title'=>"#{student_class.name.capitalize}" %>
如果您要放入HTML中的任何值需要转义以防止注入,您可以使用“h”。类似于:#{h(student_class.name.capitalize)}
。