Question

I have a form in rails (3.2) where I hide some fields depending on the update value of some comboboxes.

For this, I'm currently using javascript, here's an example:

$('#sample_pcris').change(function(){
        if( $(this).val() === "Nao" ) {
                $(".resultpcr").hide();
            } else {
                $(".resultpcr").show();   
           } 
        return false;
    });

This works well when I'm creating the record, but I want to make this "persistent" and "client independent", that is, imagine I create the record and some of the fields are hidden, if someone else tries to edit that record later I want the same fields to remain hidden unless he changes the value of the combobox...

What's the best approach for this? Serialize in javascript? Or do you suggest something else?

All ideas are welcome :) Thank you

Était-ce utile?

La solution

There are (at least) two ways to do this. Either you use JavaScript to hide the field after the page has loaded or you hide it right in the template.

The first approach would look something like:

function handleHiding() {
    if( $('#sample_pcris').val() === "Nao" ) {
        $(".resultpcr").hide();
    } else {
        $(".resultpcr").show();   
    } 
}

$(document).ready(function() {
    handleHiding();

    $('#sample_pcris').change(function(){
        handleHiding();
    });
});

Second would look like (I am using pretty much random code, but it should give you the idea):

<% if @sample_pcris == 'Nao' %>
    <%= f.text_field :resultpcr, class: 'resultpcr', style: 'display: none' %>
<% else %>
    <%= f.text_field :resultpcr, class: 'resultpcr' %>
<% end %>

Untested, obviously.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top