所以我想出了一种方法来做到这一点,但有没有更简单的方法呢?
我想要做的只是在 %th 标签之后添加 .class 如果 params[:sort] == sortBy,我真的需要在辅助方法中使用其余的 HAML 吗?
这是我的 helper.rb 文件中的辅助方法:
def yellow?(sortBy,name,id)
haml_tag :th, class: "#{'hilite' if params[:sort]== sortBy}" do
haml_concat link_to name, movies_path(sort: sortBy),{:id => id}
end
end
这是来自我的 HAML 文件:
%tr
- yellow?("title","Movie Title","title_header")
%th Rating
最佳答案
你有没有试过这个解决方案:
%tr
%th{ :class => if params[:sort] == 'sortBy' then 'hilite' end }
= link_to "Movie Title", movies_path(:sort => 'title'), :id => "title_header"
%th Rating
你可以移动这个语句:
if params[:sort] == 'sortBy' then 'hilite' end
给 helper 。看看我的类似回答:haml two spaces issue .
关于ruby-on-rails - 使用 Rails 中的辅助方法向 HAML 标记添加动态属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10902377/