Establecer condicionalmente el estilo CSS desde el controlador ruby

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'

Mi pregunta es simplemente: How and where could I dynamically set the class to %th.hilite if the header is clicked?

preguntado el 10 de marzo de 12 a las 13:03

You have to do this in Javascript, not in ruby. -

You don't have to and shouldn't use JavaScript to do this. Besides that you ought do the SaaS homework #2 by yourself. Note: The presented answer is correct. -

You should not be posting questions directly from the saas-class.org homework 2 assignment here. You should be asking how to do something.. but not for the exact answer to your question. -

Most importantly you should NOT have posted the code above that directly shows how to answer part of the question. Its just bad behaviour. -

It's pretty disappointing that when I search for information about conditional rendering in haml that this is the first link on google. Everyone who signed up for that course agreed to the honour code, it's just a shame that some people don't have any personal honour anymore. -

5 Respuestas

You can bind it from the view directly:

%th{:class => ("hilite" if @sort == "title")}= link_to 'Movie Title'...

However, you will also have to declare the @sort instance variable in your controller, and set it to either título or ,, according to which column you are sorting. (i.e. @sort = params[:sort])

respondido 10 mar '12, 18:03

actually you dont have to declare an instance variable, you can simply use params[:sort] - Sudhi

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

Respondido 07 ago 12, 22:08

If you are wondering why the ||session[:sort], it is because you may click on something other than a sortable column, in which case you do not want sorting to become undone because your params hash has expired. You will also need a little logic in controller like "if params[:sort] != session[:sort] then session[:sort] = sort" type of thing - mehtunguh

I'm trying to understand this answer. Where in the docs can I read about "what?" to understand how the title_header can have different values for each th? - También

BTW as long as values of your param match column name - no need to code each one

def index
    @by=params[:by]
    @movies = Movie.order(params[:by]).all
  end

respondido 12 mar '12, 03:03

Javascript or jquery is not required..

The following HAML will work

%th{:class=>('title' == @sortby)?'hilite':""}= link_to  'Movie Title', movies_path(:sort => 'title'), :id => 'title_header'

Respondido el 27 de Septiembre de 12 a las 03:09

You should use JavaScript to do this. I think it's good idea for you to use jQuery instead of pure JavaScript. Here are some examples

http://api.jquery.com/click/

http://api.jquery.com/css/

respondido 10 mar '12, 13:03

Neither JavaScript nor jQuery is required for this. - jjeaton

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.