Conditionally setting CSS style from ruby controller

前端 未结 5 1637
没有蜡笔的小新
没有蜡笔的小新 2021-02-01 11:07

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

5条回答
  •  自闭症患者
    2021-02-01 11:51

    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
    

提交回复
热议问题