Problems with repopulating select options in Rails when returning an error form

I have a form with two selection options - frequency and duration. When errors appear in the form and are returned to the browser, the selection parameters are not re-populated by the selected users, even if the return values ​​for those fields match the parameter values ​​in the selection. In addition, when the form is returned, these fields are not marked as having errors, even if their values ​​are empty.

Here's the frequency and duration fields in Rails

<%= frequency_select c, :frequency %>
<%= duration_select c, :duration %>

      

Frequency_select method -

def frequency_select(f, method)
    options = [["day", 1.day], ["other day", 2.days], ["week", 1.week]]
    f.select method, options, :include_blank => true
end

      

And the long_select method is -

def duration_select(f, method, unit="day" )
    values, units = *case unit
        when "day" : [[[5, 5], [15, 15], [30, 29]], "days"]
        when "other day" : [[[15, 15], [30, 29], [45,45]], "days"]
        when "week" : [[[4, 29], [6, 43], [8, 57]], "weeks"]
    end
    f.select method, values.map {|(label, i)| ["#{label} #{units}", i.days]}, :include_blank => true
end

      

If you enter a value in one or both of these fields and submit the form without part of it (any part of it), the form is returned to the user (as you would expect), but the duration and frequency of the field will not be re-populated with the user's choice.

If I add this bit of code to the form

<p><%= @challenge.attributes.inspect %></p>

      

I can see this is for duration and frequency when the form is returned to the browser:

"duration"=>3888000, "frequency"=>172800

      

These values ​​correspond to the values ​​of the parameters in the selection fields.

Is there something special in Rails that needs to be done in order for the select boxes to be repopulated using custom settings? Any thoughts on what might be the problem or what should I try next?

Help is greatly appreciated!

-Rick

PS If you look at some of the other questions, you will notice that I have asked this in the past. At one point, I thought the form would return values ​​for frequency and duration in days, not seconds, but that is not the case.

PPS Here's some more information that might make a difference, but my tests show it probably isn't. (Though, I'm a bit new to this, so don't take my word for it.)

These two fields are linked together using the jquery cascade plugin.

Javascript is enabled on the page (not in a separate file) and some of the js are generated by Rails.

First, here are the scripts that appear in the browser. The first is a script to generate options for choosing the duration, and the second is a script required by the Cascade plugin to run the field chain.

<script type="text/javascript">
    var list1 = [
        {'When':'86400','Value':' ','Text':' '},
        {'When':'172800','Value':' ','Text':' '},
        {'When':'604800','Value':' ','Text':' '},
        {'When':'86400','Value':'432000','Text':'5 days'},
        {'When':'86400','Value':'1296000','Text':'15 days'},
        {'When':'86400','Value':'2505600','Text':'30 days'},
        {'When':'172800','Value':'1296000','Text':'15 days'},
        {'When':'172800','Value':'2505600','Text':'30 days'},
        {'When':'172800','Value':'3888000','Text':'45 days'},
        {'When':'604800','Value':'2505600','Text':'4 weeks'},
        {'When':'604800','Value':'3715200','Text':'6 weeks'},
        {'When':'604800','Value':'4924800','Text':'8 weeks'}
    ];

    function commonTemplate(item) {
        return "<option value='" + item.Value + "'>" + item.Text + "</option>"; 
};

function commonMatch(selectedValue) {
        return this.When == selectedValue; 
};
</script>

<script type="text/javascript">
    jQuery(document).ready(function(){
        jQuery("#challenge_duration, #user_challenge_duration").cascade("#challenge_frequency, #user_challenge_frequency",{
            list: list1,            
            template: commonTemplate,
            match: commonMatch          
        })
    });
</script> 

      

And here is a bit of the first script, just like in the erb file, you can see that some of the script is generated by Rails

<%= 
    [ [1.day, [[5, 5], [15,15], [30, 29]], "days"],
    [2.days, [[15, 15], [30, 29], [45, 45]], "days"],
    [1.week, [[4, 29], [6, 43], [8, 57]], "weeks"]].map do |(frequency, durations, unit)|

    durations.map do |(label, value)| 
        "{'When':'#{frequency}','Value':'#{value.days}','Text':'#{label} #{unit}'}"
    end 
    end.join(",\n")
-%>

      

Now, the reason I don't think it matters if the duration is generated with JS is

  • the problem still exists if i remove all JS
  • the problem also affects the frequency field whose parameters are not generated by JS
+1


a source to share


1 answer


Allows to run the frequency_select test first Change the view code as shown below,

<%= frequency_select @challenge, :frequency %>

      



Change the helper method for frequency_select to,

def frequency_select(objct, method)
    options = [["day", 1.day], ["other day", 2.days], ["week", 1.week]]
    select objct, method, options, :include_blank => true
end

      

0


a source







All Articles