I am using the gem 'bootstrap3-datetimepicker-rails', '~> 4.17'
so that the user can select a date and time, which is passed to my controller and included in the model's attributes. However, when I add load_and_authorize_resource
to the controller (using cancancan
), I get the following error.
argument out of range
The error is happening because it is passing the date January 27, 2015, but the date and month numbers are switched, and it doesn't like getting a date with month=27
, hence the out of range
error. However, this error is not happening in my controller, and the whole process works fine when I remove load_and_authorize_resource
from the controller.
How can I get this working (avoiding the error) while still authorizing the controller with cancancan?
I am using Rails 4.1.4 and Ruby 2.0.0. Any guidance would be much appreciated, thanks!
Edit: Adding code.
damages/new.html.erb
<%= simple_form_for([@car, @damage], html: {class: 'create-damage-form'}) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<span class='input-group date damage-datepicker'>
<%= f.text_field :date_of_damage, class: "form-control" %>
<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
</span>
</br>
<%= f.text_area :description, class: 'form-control form-solo-field', placeholder: 'Enter a description of the damage...' %>
</div>
<div class="form-actions">
</br>
<%= f.submit "Submit", class: "btn btn-primary btn-md" %>
</div>
<% end %>
damages_controller.rb
def create
if !params[:damage][:date_of_damage].nil?
date_of_damage = params[:damage][:date_of_damage]
else
redirect_to :back, alert: 'Enter the date of damage.'
return
end
begin
# auth_time = DateTime.strptime(auth_time, "%m-%d-%Y %H:%M%S")
date_of_damage = DateTime.strptime(date_of_damage,'%m/%d/%Y')
rescue ArgumentError => e
redirect_to :back, alert: 'Enter a valid date.'
return
end
@car = Car.find(params[:car_id])
damage_params_with_date = damage_params
damage_params_with_date[:date_of_damage] = date_of_damage
@damage = @car.damages.build(damage_params_with_date)
respond_to do |format|
if @damage.save
format.html { redirect_to @car, notice: 'Damage record was successfully added.' }
format.json { render :show, status: :created, location: @car }
else
format.html { redirect_to :back, alert: 'Damage record was not added.' }
format.json { render json: @damage.errors, status: :unprocessable_entity }
end
end
end
partner_ability.rb
can :manage, Damage, :car_id => @partner.car_ids
can :create, Damage
Stack Trace:
Started POST "/cars/1/damages" for 127.0.0.1 at 2016-01-28 22:55:22 -0500
Processing by DamagesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"gc5CPvSxAjwJfX4ekjEuJ1eZml9aoPxQWPfTBC2/kfc=", "damage"=>{"date_of_damage"=>"01/28/2016 10:55 PM", "description"=>"test"}, "commit"=>"Submit", "car_id"=>"1"}
Completed 500 Internal Server Error in 5ms
ArgumentError - argument out of range:
activesupport (4.1.4) lib/active_support/values/time_zone.rb:289:in `parse'
activesupport (4.1.4) lib/active_support/core_ext/string/zones.rb:9:in `in_time_zone'
activerecord (4.1.4) lib/active_record/attribute_methods/time_zone_conversion.rb:37:in `date_of_damage='
activerecord (4.1.4) lib/active_record/attribute_assignment.rb:45:in `_assign_attribute'
activerecord (4.1.4) lib/active_record/attribute_assignment.rb:32:in `block in assign_attributes'
activerecord (4.1.4) lib/active_record/attribute_assignment.rb:26:in `assign_attributes'
activerecord (4.1.4) lib/active_record/core.rb:455:in `init_attributes'
activerecord (4.1.4) lib/active_record/core.rb:198:in `initialize'
activerecord (4.1.4) lib/active_record/inheritance.rb:30:in `new'
cancancan (1.13.1) lib/cancan/controller_resource.rb:80:in `build_resource'
cancancan (1.13.1) lib/cancan/controller_resource.rb:61:in `load_resource_instance'
cancancan (1.13.1) lib/cancan/controller_resource.rb:32:in `load_resource'
cancancan (1.13.1) lib/cancan/controller_resource.rb:25:in `load_and_authorize_resource'
cancancan (1.13.1) lib/cancan/controller_resource.rb:10:in `block in add_before_filter'
activesupport (4.1.4) lib/active_support/callbacks.rb:440:in `block in make_lambda'
activesupport (4.1.4) lib/active_support/callbacks.rb:160:in `block in halting'
activesupport (4.1.4) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.4) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.4) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.4) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.4) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.4) lib/active_support/callbacks.rb:86:in `run_callbacks'
actionpack (4.1.4) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.1.4) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.1.4) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.1.4) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.4) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.4) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.1.4) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.1.4) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.1.4) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.1.4) lib/abstract_controller/base.rb:136:in `process'
actionview (4.1.4) lib/action_view/rendering.rb:30:in `process'
actionpack (4.1.4) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.1.4) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.1.4) lib/action_controller/metal.rb:232:in `block in action'
actionpack (4.1.4) lib/action_dispatch/routing/route_set.rb:82:in `dispatch'
actionpack (4.1.4) lib/action_dispatch/routing/route_set.rb:50:in `call'
actionpack (4.1.4) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.1.4) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.4) lib/action_dispatch/routing/route_set.rb:678:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
meta_request (0.3.4) lib/meta_request/middlewares/app_request_handler.rb:13:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
meta_request (0.3.4) lib/meta_request/middlewares/meta_request_handler.rb:13:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/rack/agent_hooks.rb:30:in `traced_call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/rack/browser_monitoring.rb:32:in `traced_call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/rack/developer_mode.rb:48:in `traced_call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
rack (1.5.5) lib/rack/etag.rb:23:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
rack (1.5.5) lib/rack/conditionalget.rb:35:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
rack (1.5.5) lib/rack/head.rb:11:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
actionpack (4.1.4) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
actionpack (4.1.4) lib/action_dispatch/middleware/flash.rb:254:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
rack (1.5.5) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.5) lib/rack/session/abstract/id.rb:220:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
actionpack (4.1.4) lib/action_dispatch/middleware/cookies.rb:560:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
activerecord (4.1.4) lib/active_record/query_cache.rb:36:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
activerecord (4.1.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
activerecord (4.1.4) lib/active_record/migration.rb:380:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
actionpack (4.1.4) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.4) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.4) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
actionpack (4.1.4) lib/action_dispatch/middleware/reloader.rb:73:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
actionpack (4.1.4) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
better_errors (2.1.1) lib/better_errors/middleware.rb:84:in `protected_app_call'
better_errors (2.1.1) lib/better_errors/middleware.rb:79:in `better_errors_call'
better_errors (2.1.1) lib/better_errors/middleware.rb:57:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
rack-contrib (1.4.0) lib/rack/contrib/response_headers.rb:17:in `call'
meta_request (0.3.4) lib/meta_request/middlewares/headers.rb:16:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
actionpack (4.1.4) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
actionpack (4.1.4) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
railties (4.1.4) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.4) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.4) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.4) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.4) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.4) lib/rails/rack/logger.rb:20:in `call'
quiet_assets (1.1.0) lib/quiet_assets.rb:27:in `call_with_quiet_assets'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
actionpack (4.1.4) lib/action_dispatch/middleware/request_id.rb:21:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
rack (1.5.5) lib/rack/methodoverride.rb:21:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
rack (1.5.5) lib/rack/runtime.rb:17:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
activesupport (4.1.4) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
rack (1.5.5) lib/rack/lock.rb:17:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
actionpack (4.1.4) lib/action_dispatch/middleware/static.rb:64:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
rack (1.5.5) lib/rack/sendfile.rb:112:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
railties (4.1.4) lib/rails/engine.rb:514:in `call'
railties (4.1.4) lib/rails/application.rb:144:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
rack (1.5.5) lib/rack/content_length.rb:14:in `call'
thin (1.6.4) lib/thin/connection.rb:86:in `block in pre_process'
thin (1.6.4) lib/thin/connection.rb:84:in `pre_process'
thin (1.6.4) lib/thin/connection.rb:53:in `process'
thin (1.6.4) lib/thin/connection.rb:39:in `receive_data'
eventmachine (1.0.8) lib/eventmachine.rb:193:in `run'
thin (1.6.4) lib/thin/backends/base.rb:73:in `start'
thin (1.6.4) lib/thin/server.rb:162:in `start'
rack (1.5.5) lib/rack/handler/thin.rb:16:in `run'
rack (1.5.5) lib/rack/server.rb:264:in `start'
railties (4.1.4) lib/rails/commands/server.rb:69:in `start'
railties (4.1.4) lib/rails/commands/commands_tasks.rb:81:in `block in server'
railties (4.1.4) lib/rails/commands/commands_tasks.rb:76:in `server'
railties (4.1.4) lib/rails/commands/commands_tasks.rb:40:in `run_command!'
railties (4.1.4) lib/rails/commands.rb:17:in `<top (required)>'
bin/rails:4:in `<main>'