Hi there i just need to require the param start_hour from my form so that i compare it to the database and check if it exist, but when i requiere the param that is in the form like
<%= f.datetime_select :start_hour, { :order => [:month, :day, :year, :hour, :minute], :prompt => { :day => 'Dia', :month => 'Mes', :year => 'Ano', :hour => 'Hora', :minute => 'Minuto' } }, {:class => "form-control", :style => "display: inline-block;
width: auto;
vertical-align: middle;"} %>
i get that as nil in my controller, how can i require a datetime form then? many thanks. Here is my controller
def create
@start_hour = params[:courtesie][:start_hour]
@lesson_check = Lesson.find_by(start_date: @start_hour)
if @lesson_check.present?
@new_courtesie = Courtesie.new(courtesie_params)
@new_schedule = Schedule.new()
@new_schedule.options = params[:courtesie][:name]
@new_schedule.lesson_id = @lesson_check.id
@new_schedule.user_id = current_user.id
if @new_courtesie.save && @new_schedule.save
flash[:success] = "Cortesia agendada"
redirect_to cortesias_path
end
else
flash[:warning] = @start_hour
redirect_to cortesias_path
end
end
form code
<%= form_for(:courtesie, html: { class: "form-horizontal" } ) do |f| %>
<div class="form-group">
<label class="control-label col-md-2" for="sitename"> Nombre cortesia
</label>
<div class="col-md-9">
<%= f.text_field :name, class: 'form-control' %>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label" for="sitename"> Fecha cortesia</label>
<div class="col-lg-10">
<%= f.datetime_select :start_hour, { :order => [:month, :day, :year, :hour, :minute], :prompt => { :day => 'Dia', :month => 'Mes', :year => 'Ano', :hour => 'Hora', :minute => 'Minuto' } }, {:class => "form-control", :style => "display: inline-block;
width: auto;
vertical-align: middle;"} %>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="sitename"> Telefono </label>
<div class="col-md-5">
<%= f.number_field :phone, class: 'form-control' %>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="sitename"> Email </label>
<div class="col-md-5">
<%= f.text_field :email, class: 'form-control' %>
</div>
</div>
<div class="form-group">
<div class="col-lg-9 col-lg-offset-2">
<%= f.submit "Guardar", class: "btn btn-primary" %>
<button type="reset" class="btn btn-default">Reiniciar</button>
</div>
</div>
<% end %>
Check if you set the parameter in your strong parameters.
def courtesie_params
params.require(:courtesie).permit(:start_hour, ...)
end
If it's not permitted it'll be nil.
I assume you have permitted the start_hour
atrribute in the strong parameters. Unlike the other fields, you can't access the values in a date_time field like params[:courtesie][:start_hour]
To have a sense of what I am talking about, replace the code in your create
method with
def create
render text: courtesie_params
end
Now if you submit the form, the form data would be displayed as the response.
This is what the form data would look like
{"name"=>"what", "start_time(1i)"=>"2016", "start_time(2i)"=>"8", "start_time(3i)"=>"9", "start_time(4i)"=>"23", "start_time(5i)"=>"40"}
Well, you could have a lot more values but lets focus on the datetime values. You can see where the selected values are stored. Thats why params[:courtesie][:start_hour]
is nil.
So, you need to retrieve the values like
datetime_params = params[:courtesie]
year = datetime_params["start_time(1i)"]
month = datetime_params["start_time(2i)"]
day = datetime_params["start_time(3i)"]
hour = datetime_params["start_time(4i)"]
minute = datetime_params["start_time(5i)"]
Now to compare two datetimes, you actually need to convert the collected values to a datetime
.
@start_hour = DateTime.new(year, month, day, hour, minute)
Now you can use this to query a datetime value and compare with other datetime values.
Hope this helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.