Blueone
Blueone

Reputation: 83

Selected option with a select_tag

I have a select_tag, like this:

<%= form_tag orders_path do %>               
           <script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
            data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
            data-label="Pay by CB"
            data-email="<%= current_user.email %>"
            data-name="Order"
            data-locale=auto>
          </script>
   <!-- url question -->
      <div class="widthfull">
        <p class="question">What's the URL ?</p>
        <div class="widthfull"><%= text_area_tag(:url, "", class: "answer textarea widthfull calcul_checkout", rows:"1") %></div>
      </div>
   <!-- language_ov question -->
     <div> 
      <p class="question">
      What's the language of the video ?
      </p> 
     <div class="flex"> 
      <span class="answer language_ov">></span> 
      <span>
       <%= select_tag(:language_ov, 
         options_for_select([['Français', 'FR'],['Anglais', 'EN']]), 
         class:"answer language_ov calcul_checkout chosen-select")%>
      </span> 
     </div> 
   </div> 
<% end %>

But if my user don't re-select the choice by default params[:language_ov] is equal to nil.

I want to give the default value 'FR' for the params[:language_ov] if my user does not select anything.

Here the model validation Request.rb

class Request < ApplicationRecord
  belongs_to :user
  belongs_to :order
end

Where i'm using the params in the controller

.. @request = Request.new(language_ov: params[:language_ov], language_request: language, export_option: params[:export_option]||'no') ..

Here the server log (no params for language_ov):

Started POST "/orders" for ::1 at 2017-08-07 22:20:22 +0200 Processing by OrdersController#create as HTML'   
Parameters: {"utf8"=>"✓", "authenticity_token"=>"*************",
"url"=>"www.url.com",
 "video_title"=>"Project name",
 "lengthminutes"=>["6"], "lengthsecondes"=>[""],
 "language_request"=>["EN"],
 "project_comment"=>"",
 "stripeToken"=>"************",
 "stripeTokenType"=>"card",
 "stripeEmail"=>"[email protected]"}

Apparently select_tag don't support selected option. Should i use select methode ? Could you help me to transform my select_tag into select ?

Upvotes: 0

Views: 173

Answers (1)

Blueone
Blueone

Reputation: 83

I found the problem. I had a javascript to pre-fill the form if the user already start to complete the form before. I stock the data with the localStorage variable. So when a user arrive on the page i check if the localStorage exist. And this was the problem because it's rewrite the selected value every time. I add a condition to execute this line (and it's working). I will find an other solution to save the form and pre-fill the form when the user come back. $("#language_request").val(localStorage.language_request);

Upvotes: 0

Related Questions