jeudi 23 juillet 2015

How to avoid refreshing the page using a form_tag in Rails 4

I have a form with slide bars which have values per default (value in the code under ). When I click the submit button it calculates the results correctly or when I change the default values manually. The thing is when changing the default values manually, it refreshes again the page and set the values to their default value.

The form is this one, which I have in a partial view for ordering purposes.

<%= form_tag() do %>


          <h2>J  <a id="dialog-1" href="#" ><i class="fa fa-info-circle"></i></a></h2>
          <div id="slider-jahresverbrauch"></div>
          <%= text_field_tag "user_entry_module1", params["user_entry_module1"] || "", id: "amount-jahresverbrauch", disable: true, class: "amount", size: 2 %>  
          <span>kWh</span>

          <h2>PV  <a id="dialog-2" href="#" ><i class="fa fa-info-circle"></i></a></h2>
          <div id="slider-pvgrosse"></div>
          <%= text_field_tag "user_entry_module2", params["user_entry_module2"] || "", id: "amount-pvgrosse", class: "amount", size: 2 %>  
          <span>kWp</span>

          <h2>S  <a id="dialog-3" href="#" ><i class="fa fa-info-circle"></i></a></h2>
          <div id="slider-strompreis"></div>
          <%= text_field_tag "user_entry_module3", params["user_entry_module3"] || "", id: "amount-strompreis", class: "amount", size: 2 %> 
          <span>cent/kWh</span>

          <h2>St  <a id="dialog-4" href="#" ><i class="fa fa-info-circle"></i></a></h2>
          <div id="slider-strompreissteigerung"></div>
          <%= text_field_tag "user_entry_module4", params["user_entry_module4"] || "", id: "amount-strompreissteigerung", class: "amount", size: 2 %>
          <span>%</span>

          <h2>E  <a id="dialog-5" href="#" ><i class="fa fa-info-circle"></i></a></h2>
          <div id="slider-ertrag"></div>
          <%= text_field_tag "user_entry_module5", params["user_entry_module5"] || "", id: "amount-ertrag", class: "amount", size: 2 %>
          <span>kWh/kWp</span>

          <h2>D  <a id="dialog-6" href="#" ><i class="fa fa-info-circle"></i></a></h2>
          <div id="slider-degression"></div>
          <%= text_field_tag "user_entry_module6", params["user_entry_module6"] || "", id: "amount-degression", class: "amount", size: 2 %>
          <span>pro Jahr %</span>

          <h2 class="text-input">P  <a id="dialog-7" href="#" ><i class="fa fa-info-circle"></i></a></h2>
          <%= text_field_tag "user_entry_module7", params["user_entry_module7"] || "", value: "1600", id: "", class: "text-input last-input", size: 2 %>
          <span class="text-input anlagepreis-unit" >pro kWp</span>

          <h2 class="text-input">E  <a id="dialog-8" href="#" ><i class="fa fa-info-circle"></i></a></h2>
          <%= text_field_tag "user_entry_module8", params["user_entry_module8"] || "", value: "12", id: "", class: "text-input last-input", size: 2 %>
          <span class="text-input anlagepreis-unit" >cent/kWh</span>

          <h2 class="">?  <a id="dialog-9" href="#" ><i class="fa fa-info-circle"></i></a></h2>
          <%= text_field_tag "user_entry_module9", params["user_entry_module9"] || "", value: "ja", id: "", class: "", size: 2 %>

          <%= submit_tag "Berechnen", class: "send_button_1" %>    

  <% end %> 

I have the application.html.erb and front_end.html.erb views.

In the front_end.html.erb the structure is more or less like this:

<%= render "partials/form" %> 

 <script>

  $(function() {

    /Jahresstromverbrauch
    $( "#slider-jahresverbrauch" ).slider({     
      value:3300,
      min: 2000,
      max: 8800,
      step: 100,
      slide: function( event, ui ) {
        $( "#amount-jahresverbrauch" ).val(ui.value);
      }

    });
    $( "#amount-jahresverbrauch" ).val($( "#slider-jahresverbrauch" ).slider( "value" ));

// in this way I define the other slide bars.

// Ajax call to code1
// Ajax call to code 2 (depends on the results from code1)
// Plotting with highcharts 



});
</script>

routes:

Prefix Verb URI Pattern                               Controller#Action

api_v2_energycalcsb_get_values POST /api/v2/energycalcsb/get_values(.:format) api/v2/energycalcsb#get_values
    api_v3_financecalc_get_eco POST /api/v3/financecalc/get_eco(.:format)     api/v3/financecalc#get_eco
         calculation_front_end POST /calculation/front_end(.:format)          calculation#front_end
                          root GET  /                                         calculation#front_end

Question: How can I avoid that after updating the values it maintains these values after clicking the submit button? I noticed that when clicking the address is called again (loading application.html and front_end.html.erb)

Aucun commentaire:

Enregistrer un commentaire