dimanche 22 novembre 2015

Issues with Createing a form update button from a controller method in Rails 4

Hey all i am building out a CAD App in Rails 4 Ruby 2.

Background:

I have a form field that is a f.time_select, i would like to replace that with a button that says on scene. When Pressed I would like it to update the unit_on_scene column in my calls table and then redirects me to the show.html.erb page.

My Button Looks Like:

<%= link_to "On Scene", update_unit_on_scene_call_path class: 'btn btn-success btn-sm' %>

My routes.rb Looks Like:

  resources :calls do 
    collection do
      get 'history'
    end
    member do
      patch :update_unit_on_scene
    end
  end

That then gives me this link in Rake Routes:

update_unit_on_scene_call PATCH  /calls/:id/update_unit_on_scene(.:format) calls#update_unit_on_scene

My Controller Method Looks Like:

def update_unit_on_scene
  @call = Call.find(params[:id])
  @call.unit_on_scene = DateTime.now
  @call.save

  respond_to do |format|
    if @call.update(call_params)
      format.html { redirect_to @call, notice: "On Scene Time Successfully Updated. - You Are Now Logged Out Of Service" }
    else
      format.html { render action: 'edit' }
    end
  end
end

The Problem I have right now is that when I Push the button it gives the following error:

No route matches [GET] "/calls/3/update_unit_on_scene"

as if it is looking for another page??

Any help would be greatly appreciated as I have never ventured down this road before.

Thanks.

EDIT # 1:

Associated Warning when Button Clicked:

ActionController::ParameterMissing in CallsController#update_unit_on_scene
param is missing or the value is empty: call

Extracted source (around line #107):
105
106
107
108
109
110

    # Never trust parameters from the scary internet, only allow the white list through.
    def call_params
      params.require(:call).permit(:call_time, :status, :primary_type, :secondary_type, :site, :address, :unit_1, :unit_2, :unit_3, :unit_4, :call_details, :unit_on_scene, :unit_clear, :call_num, :site_id, :user_id, :unit2_os, :unit2_cl, :unit3_os, :unit3_cl, :unit4_os, :unit4_cl)
    end
end

Server Log:

Started PATCH "/calls/3/update_unit_on_scene" for ::1 at 2015-11-22 01:38:49 -0700
      ActiveRecord::SchemaMigration Load (0.2ms)  SELECT "schema_migrations".* FROM "schema_migrations"
    Processing by CallsController#update_unit_on_scene as HTML
      Parameters: {"authenticity_token"=>"hH4juJAS7bxs+HHPjSuEDLpucwAh2i2QODH8DvF3JQVxhzhet1AcAs6xzib6AezrlLJ3yZrHLea5ey8206gIqA==", "id"=>"3"}
      Call Load (0.4ms)  SELECT  "calls".* FROM "calls" WHERE "calls"."id" = $1 LIMIT 1  [["id", 3]]
       (0.1ms)  BEGIN
      SQL (0.6ms)  UPDATE "calls" SET "unit_on_scene" = $1, "updated_at" = $2 WHERE "calls"."id" = $3  [["unit_on_scene", "2015-11-22 08:38:50.027783"], ["updated_at", "2015-11-22 08:38:50.031354"], ["id", 3]]
       (6.4ms)  COMMIT
    Completed 400 Bad Request in 48ms (ActiveRecord: 9.9ms)

    ActionController::ParameterMissing (param is missing or the value is empty: call):
      app/controllers/calls_controller.rb:107:in `call_params'
      app/controllers/calls_controller.rb:89:in `block in update_unit_on_scene'
      app/controllers/calls_controller.rb:88:in `update_unit_on_scene'


      Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/_source.erb (9.0ms)
      Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (3.2ms)
      Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.5ms)
      Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (53.4ms)
      Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/web-console-2.2.1/lib/web_console/templates/_markup.html.erb (0.6ms)
      Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/web-console-2.2.1/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.6ms)
      Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/web-console-2.2.1/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.7ms)
      Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/web-console-2.2.1/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.9ms)
      Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/web-console-2.2.1/lib/web_console/templates/console.js.erb within layouts/javascript (42.8ms)
      Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/web-console-2.2.1/lib/web_console/templates/main.js.erb within layouts/javascript (0.8ms)
      Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/web-console-2.2.1/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.6ms)
      Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/web-console-2.2.1/lib/web_console/templates/index.html.erb (96.0ms)

Aucun commentaire:

Enregistrer un commentaire