从ruby控制器有条件地设置CSS样式 [英] Conditionally setting CSS style from ruby controller
问题描述
我试图动态地改变(如果它点击)一个普通的表头(这是一个链接)到另一个定义的CSS类'th.hilite'。此链接只是对此列进行排序,每次用户对列表进行排序时,标题都应突出显示。
I'm trying to dynamically change (if it got clicked) a normal table header (which is a link) to another defined CSS class 'th.hilite'. This link simply sorts this column and the header should got highlighted every time a user sorts the list.
应该更改相关类别的视图,如下所示:
The view where the class in question should be changed, looks like this:
%table#mytable
%thead
%tr
%th= link_to 'Title', mytable_path(:sort => 'title'), :id => 'title_header'
我的问题是:如何以及在哪里可以动态设置类
My question is simply: How and where could I dynamically set the class to %th.hilite if the header is clicked?
推荐答案
注意不要将逻辑(即使是条件)放在你的意见,如果你能找到一个方法。为了避免这种常见错误,您需要充分利用params和会话哈希,并在控制器中设置适当的变量。
Be careful not to put logic (even conditionals) in your views if you can find a way around it. In order to avoid this common mistake, you need to make good use of the params and session hashes and set appropriate variables in controllers
# In your view
%th{:class => @title_header}= link_to 'Title', my_path(:sort => 'title'), :id => 'title_header'
# In your controller
sort = params[:sort] || session[:sort]
if sort == 'title'
ordering = {:order => :title}
end
这篇关于从ruby控制器有条件地设置CSS样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!