I am trying to implement a datetime_select option for a form. I was using a simple date selector like below before hand but the requirements have changed.
<%= f.datetime_select :ends_at, :start_year => Date.current.year %>
What I want to implement instead is to give users only three options as: "One hour", "Two hours", "Three hours" for selection. The corresponding values for the chosen option sent as POST through the form would be calculated from the time the submit button is pressed.
How can I give these options for the users for a datetime data type ?
Okay you can do something like that:
In your Model define:
TIMER = [{"time"=>"One Hour", "val"=>"1"}, {"time"=>"Two Hours", "val"=>"2"}, {"time"=>"Three Hours", "val"=>"3"}]
In your form use a different select field:
<%= f.select :time_to_end, options_for_select(Model::TIMER.map { |obj| [obj['time'], obj['val']] }) %>
Then in the model allow time_to_end parameter as a attr_accesible.
attr_accessible :time_to_end
Also, add a before save callback to change the ends_at field:
before_save :update_ends_at_field
private
def update_ends_at_field
self.ends_at = Time.now + time_to_end.to_i.hours
end
I didn't tested the code above. Hope you got the idea.
I ended up using
<%= f.select :ends_at, options_for_select(ApplicationHelper::TIMER.map { |obj| [obj['time'], obj['val']] })%>
in the form itself.
Where Timer is defined as:
module ApplicationHelper
TIMENOW = DateTime.now
TIMER = [{"time"=>"Half Hour", "val"=> TIMENOW + 0.5.hour},{"time"=>"One Hour", "val"=> TIMENOW + 1.hour}, {"time"=>"One Day", "val"=> TIMENOW + 24.hour}, {"time"=>"Three Days", "val"=> TIMENOW + 3.day}, {"time"=>"Seven Days", "val"=> TIMENOW + 7.day}]
end
in the ApplicationHelper
Related
I have a start_dt and an end_dt object that are of type datetime.
How can I loop from the start_dt, incrementing by 1 day each time until I reach the end date?
You can use the upto and downto methods on Date and DateTime objects:
start_dt = DateTime.parse('2018-01-01')
end_dt = DateTime.parse('2018-01-15')
start_dt.upto(end_dt) { |date| puts date }
2018-01-01T00:00:00+00:00
2018-01-02T00:00:00+00:00
2018-01-03T00:00:00+00:00
2018-01-04T00:00:00+00:00
2018-01-05T00:00:00+00:00
2018-01-06T00:00:00+00:00
2018-01-07T00:00:00+00:00
2018-01-08T00:00:00+00:00
2018-01-09T00:00:00+00:00
2018-01-10T00:00:00+00:00
2018-01-11T00:00:00+00:00
2018-01-12T00:00:00+00:00
2018-01-13T00:00:00+00:00
2018-01-14T00:00:00+00:00
2018-01-15T00:00:00+00:00
end_dt.downto(start_dt) { |date| puts date }
2018-01-15T00:00:00+00:00
2018-01-14T00:00:00+00:00
2018-01-13T00:00:00+00:00
2018-01-12T00:00:00+00:00
2018-01-11T00:00:00+00:00
2018-01-10T00:00:00+00:00
2018-01-09T00:00:00+00:00
2018-01-08T00:00:00+00:00
2018-01-07T00:00:00+00:00
2018-01-06T00:00:00+00:00
2018-01-05T00:00:00+00:00
2018-01-04T00:00:00+00:00
2018-01-03T00:00:00+00:00
2018-01-02T00:00:00+00:00
2018-01-01T00:00:00+00:00
How about this?
current_date = object.start_dt
end_date = object.end_dt
loop do
## do something
## (`:beginning_of_day` and `:end_of_day` may help you)
current_date += 1.day
break if current_date > end_date
end
Try to the following
start_date = Time.now - 30.day
=> 2017-12-16 09:19:51 +gmt
end_date = Time.now
=> 2018-01-15 09:19:44 +gmt
start_date = Date.parse "#{start_date}"
end_date = Date.parse "#{end_date}"
date_count = (end_date - start_date).to_i
=> 30
<% (1..date_count).each do |i| %>
#Code to display using <%= i %> that you want to display
<% end %>
Hope to help
Try these:
(DateTime.parse('2018-01-01')..DateTime.parse('2018-01-15')).each do |date|
puts date
end
I am trying to convert user input à la "6 months" into a Ruby method à la 6.months.
How can this be done?
I keep getting an undefined method 6.months error in line 10.
Thanks for any help.
class Coupon < ActiveRecord::Base
def self.duration_options
["3 years", "2 years", "1 year", "6 months", "3 months", "1 month"]
end
def end_at
if Coupon.duration_options.include?(duration)
method = duration.sub!(' ', '.')
time = Time.zone.now + send(method)
end
time
end
end
Your send is on the object itself, in this case it's self.send(method)
You could do like this
if Coupon.duration_options.include?(duration)
off_set = duration.split(" ")
time = Time.zone.now + off_set.first.to_i.send(off_set.last.to_sym)
end
time
I am having trouble accessing a value of select_tag in my controller. I have the following select_tag in my view:
= select_tag "yessir", options_for_select([ ["No Teardown Time","0"],["15 Minutes", "15"], ["30 Minutes", "30"], ["45 Minutes", "45"], ["60 Minutes", "60"], ["75 Minutes", "75"], ["90 Minutes","90"], ["105 Minutes", "105"],["120 Minutes","120"]], #event.teardown_time), {:prompt => 'Teardown Time'}
And I try to access it in my controller like such:
a = params["yessir"]
I have also tried
a = params[:yessir]
But in either case I keep on getting undefined local variable or method `params' for #. Any suggestions? Cheers~
On another note, how would I obtain the value if the select_tag had the following name:
= select_tag 'event[teardown_time]'
I tried params['event[teardown_time'], but it did not work - no error but value is not being obtained. Cheers~
I have an array of names. I want the first option to be blank, and the last option to be "Joe".
Here is the code:
def index
#case_managers = client.personnel_search_by_client(current_client.client_id, nil, groupMnemonic: 'reimbursement_whitelist')
#case_managers_drop_down = {}
#case_managers.each do |case_manager|
#case_managers_drop_down[case_manager.name] = case_manager.to_json
end
end
In my views I have:
= form_tag work_lists_path, :method=> 'put' do |f|
.fieldset.field-group.field-group-inline.pull-left
.field.field-text.field-required
%label= t('workflow.duplicate_claim_manager')
= select_tag('case[case_manager]', options_for_select(#case_managers_drop_down, #selected_case_manager))
This correctly gets all the names. I thought before the array I would do something like #case_managers_drop_down.push(" ") to get a blank option and then likewise for Joe. But this doesnt seem to be working. Any idea on how I can append to this array?
To get a blank option at first place in select_tag use include_blank boolean attribute like :
= select_tag('case[case_manager]', options_for_select(#case_managers_drop_down, #selected_case_manager), :include_blank => true)
OR use prompt like :
= select_tag('case[case_manager]', options_for_select(#case_managers_drop_down, #selected_case_manager), :prompt => "Please select")
For last option as joe do it like :
def index
#case_managers = client.personnel_search_by_client(current_client.client_id, nil, groupMnemonic: 'reimbursement_whitelist')
#case_managers_drop_down = {}
#case_managers.each do |case_manager|
#case_managers_drop_down[case_manager.name] = case_manager.to_json
end
#case_managers_drop_down["joe"] = ""
end
Hopefully it will work!
I have a problem with get value from :days new form.
new.html.haml
= f.select :days, [['1 month', 30], ['2 months', 60], ['3 months', 90]]
controller
def create
#bought_detail = BoughtDetail.new(bought_detail_params)
#bought_detail.entry_type = #entry_type
#bought_detail.person = current_person
if #bought_detail.entry_type.kind == 'Karnet'
#bought_detail.cost = #bought_detail.entry_type.price * (days / 30).floor
else
#bought_detail.cost = #bought_detail.entry_type.price
end
end
private
def bought_detail_params
params.require(:bought_detail).permit(:bought_data, :start_on, :end_on, :entry_type_id, :days, :person_id, :credit_card, :card_code)
end
Model
belongs_to :entry_type
belongs_to :person
before_save :set_bought_data, :set_end_on, :set_start_on
attr_accessor :credit_card, :card_code, :days
def set_bought_data
self.bought_data = Date.today
end
def set_start_on
self.start_on = bought_data if start_on.nil?
end
def set_end_on
days = "7" if days.nil?
self.end_on = Date.today + days.to_i
end
When I fill new form and click submit I get this error:
undefined local variable or method `days'
I want to deliver value from select field e.g. when I choose 2 months, value of the day will be 60 and I will can get calculate #bought_detail.cost. How to do this?
Thanks in advance!
In controller you're trying to access undefined variable days, change to accessing the model:
#bought_detail.cost = #bought_detail.entry_type.price * (#bought_detail.days.last / 30).floor
In model change to:
self.days = "7" if days.nil?
because otherwise you'll be assigning to local variable, not the attribute.
In your controller's create method. Where are you getting the days from?
#bought_detail.cost = #bought_detail.entry_type.price * (days.last / 30).floor
This bit specifically: days.last / 30. Ruby can't find days method or local variable. I guess you want to access it from params[:your_model][:days]?