0

When a user creates a vacancy I want them to be able to save either 1 range of dates or multiple separate dates.

So I have 2 models, Vacancy and Vacancyschedule.

Vacancyschedule includes vacancy_id start_date end_date start_hour end_hour (to save a range) and when it is multiple seperate dates, I just want to leave the end_date empty and have multiple entries and combine them through my vacancy_id.

This is my code in my view:

... other code to ask for vacancy params, nothing special, nothing broken ...

#Code to create entry in vacancyschedule
    <%= t.simple_fields_for :vacancyschedule do |p| %>
       <%= p.input :start_date, :as => :date_picker, :label => false%>
       <%= p.input :start_hour, as: :time, default: Time.parse('09:00')%>
       <%= p.input :end_hour, as: :time, default: Time.parse('17:00')%>
    <% end %>

And then I have some javascript that adds another exact copy one of those blocks when a user wants to add a second seperate date.

Now for my question:

The format in which they are passed is very strange. As you can see I ask for :start_date, :start_hour and :end hour and this is what I get:

{"name"=>"", "street"=>"", "description"=>"", "skill_id"=>"",
"jobtype_id"=>"", "wage"=>"", "vacancyschedule"=>
{"start_date"=>"27/10/15", "end_date"=>"", "start_hour"=>"", 
"end_hour"=>"", "start_hour(1i)"=>"2015", "start_hour(2i)"=>"10",
 "start_hour(3i)"=>"26", "start_hour(4i)"=>"21", 
"start_hour(5i)"=>"00", "end_hour(1i)"=>"2015", "end_hour(2i)"=>"10",
 "end_hour(3i)"=>"26", "end_hour(4i)"=>"17", "end_hour(5i)"=>"00"}, 
"vacancyscheduele"=>{"start_date"=>"", "start_hour(1i)"=>"2015", 
"start_hour(2i)"=>"10", "start_hour(3i)"=>"26", "start_hour(4i)"=>"09",
"start_hour(5i)"=>"00", "end_hour(1i)"=>"2015", "end_hour(2i)"=>"10",
"end_hour(3i)"=>"26", "end_hour(4i)"=>"17", "end_hour(5i)"=>"00"}, 
"tag_list"=>""}, "radio"=>"on", "commit"=>"Create Vacancy", 
"controller"=>"vacancies", "action"=>"create", "employer_id"=>"2"}

From what I can see is that they are passed in very different variables and incomplete (not all dates are passed).

Does anyone have any experience with this issue? How would I either grab these elements or how do I prevent them from being pushed in that format?

1 Answer 1

1

Eeeehhh, I hate nested forms. I always go back to the basics when I have to deal with them. Checkout these fine railscasts. The second one I think is better aimed at your problem but the first one helps understand it:

http://railscasts.com/episodes/196-nested-model-form-part-1

http://railscasts.com/episodes/197-nested-model-form-part-2

At the end of the day, you need to know what your html is generating and make sure it is what Rails is expecting in the backend. Also, you must account for the nested objects at load and save time. Your HTML as it stands, is not generating parseable objects as far as Rails is concerned. On the other hand, it is hard to deal with the crap date/time picker for Rails. I usually like to put in something that generates a better date picker. One that generates one string that is easily maps to a database field. If you are using bootstrap, I'd use the bootstrap datepicker gem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.