Mixing FormHelper methods with other methods

22 views
Skip to first unread message

Federicko

unread,
Jul 30, 2015, 11:35:28 AM7/30/15
to Ruby on Rails: Talk

Hi,


I have a simple form using form_for but I need to have a field in the form that uses select_time which is not part of the FormHelper and there lies the issue.


 <%= form_for [:admin, @restaurant, @opening_hour] do |f| %>
  <div class="field">
  <%= f.label :day %><br>
  <%= f.text_field :day %>
 </div>
 <div class="field">
  <%= f.label :start_time %><br>
  <%= select_time Time.now, prefix: :start_time %>
 </div>
<% end %>


The form above works fine except that the params passed over to the controller is separate:


params[:opening_hour]

params[:start_time]


Now as a newbie, all I have been doing in the controller is to use one params, params[:opening_hour], for all the form data. But with this form that produces two params, I don't know how to combine them both into one so my controller will work. Especially cos the params maps to the model object.


Any ideas?


Thanks in advance.




Elizabeth McGurty

unread,
Jul 30, 2015, 3:20:09 PM7/30/15
to Ruby on Rails: Talk, fedo...@gmail.com
<%= f.select_time Time.now, prefix: :start_time %>

Federicko

unread,
Jul 31, 2015, 2:41:17 PM7/31/15
to Ruby on Rails: Talk, fedo...@gmail.com
I have found the solution to this:

<%= f.label :start_time %><br>
<%= time_select("opening_hour", "start_time", :ignore_date => true) %>

Thats all I need to do to include the time select options in the form and I didn't need to do anything special in the controller to make this work as time_select automatically stores the values in the appropriate object.
Reply all
Reply to author
Forward
0 new messages