lundi 29 août 2022

rails - render form fields dynamically

I Have a Simple Form field with year and month field in the form.

<%= form_for :calender, remote: true do |f| %>
  <%= f.select :year, @year_array %>
  
  <% if :year == DateTime.now.strftime("%Y").to_i %>
    <%= f.select :month, @month_array[0..Date::MONTHNAMES.index(DateTime.now.strftime("%B"))-1] %>
  <% else %>
    <%= f.select :month, @month_array %>
  <% end %>
  
  <%= f.submit "Submit" %>
<% end %>
@year_array data contains in format [[2020, 2020], [2021, 2021], [2022, 2022]]

@month_array data contains in format 
[["January", "Jan"], ["February", "Feb"], ["March", "Mar"], ["April", "Apr"], ["May", "May"], 
 ["June", "Jun"], ["July", "Jul"], ["August", "Aug"], ["September", "Sep"], ["October", "Oct"], 
 ["November", "Nov"], ["December", "Dec"]]

How can I render this form dynamically. what I mean is when the user select the current year then month only render up to current month from January rather than whole array.

Aucun commentaire:

Enregistrer un commentaire