Using select_month in form_for

I work in Ruby on Rails and I cannot figure out how to use select_month in form_for. I'm trying to do

<%= form_for(@farm) do |f| %> <div class="field"> <%= f.label :harvest_start %><br /> <%= select_month(Date.today, :field_name => 'harvest_start') %> </div> <div class="actions"> <%= f.submit %> </div> <% end %> 

which outputs

 ["harvest_start", nil] 

Thank you for your help. sorry if the formatting is wrong.

+6
source share
2 answers

(This was answered in the comments of T. Weston Kendall, so he just answered correctly)

The following code is what I should work.

_form.html.erb

 <div class="field"> <%= f.label :harvest_start %><br /> <%= f.collection_select :harvest_start, Farm::MONTHS, :to_s, :to_s, :include_blank => true %> </div> 

farms_controller.rb

 @farm.harvest_start = params[:harvest_start] 

I also got select_month to work, but I needed :include_blank , and I didn't want to waste time figuring out what to do with nil in the array.

+2
source

To use the selection month in series with the rest of the form builder, use date_select without a day or month:

 date_select(object_name, method, options = {}, html_options = {}) 

Just use f.date_select :harvest_start, {order: [:month]} , that is, add order: [:month] or discard_year: true, discard_day: true in the options hash, as specified in docs

In favor of this, you can pass all other parameters, as in the hash setting. Sort of:

 <%= f.date_select :birth_date, {prompt: true, order: [:month]}, class: 'form-control' %> 
+2
source

Source: https://habr.com/ru/post/893223/


All Articles