1

When I send post request from my js file

$http.post('/users/:user_id/tasks/', {id: $scope.task.id, title: data});

to the create action in my controller, I get the following error.

enter image description here

with parameters

enter image description here

What does it mean?

controller

  def create
    respond_to current_user.tasks.create(task_params)
  end

  private

  def task_params
    params.require(:task).permit(:id, :title, :due_date, :priority, :complete)
  end

rake routes

         user_tasks GET    /users/:user_id/tasks(.:format)          tasks#index
                    POST   /users/:user_id/tasks(.:format)          tasks#create
      new_user_task GET    /users/:user_id/tasks/new(.:format)      tasks#new
     edit_user_task GET    /users/:user_id/tasks/:id/edit(.:format) tasks#edit
          user_task GET    /users/:user_id/tasks/:id(.:format)      tasks#show
                    PATCH  /users/:user_id/tasks/:id(.:format)      tasks#update
                    PUT    /users/:user_id/tasks/:id(.:format)      tasks#update
                    DELETE /users/:user_id/tasks/:id(.:format)      tasks#destroy

rails console log

Started POST "/users/:user_id/tasks/" for 127.0.0.1 at 2015-08-24 15:46:23 +0300
Processing by TasksController#create as HTML
  Parameters: {"id"=>1, "title"=>"some text", "user_id"=>":user_id", "task"=>{"id"=>1, "title"=>"some text"}}
  User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
   (0.2ms)  begin transaction
   (0.1ms)  commit transaction
Completed 500 Internal Server Error in 11ms (ActiveRecord: 0.8ms)

NoMethodError (undefined method `upcase' for #<Task:0x007f91f513a4c8>):
  app/controllers/tasks_controller.rb:22:in `create'


  Rendered /home/alex/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/_source.erb (9.9ms)
  Rendered /home/alex/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (4.4ms)
  Rendered /home/alex/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.6ms)
  Rendered /home/alex/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (39.4ms)
Cannot render console with content type application/jsonAllowed content types: [#<Mime::Type:0x00000003c77a60 @synonyms=["application/xhtml+xml"], @symbol=:html, @string="text/html">, #<Mime::Type:0x00000003c57cb0 @synonyms=[], @symbol=:text, @string="text/plain">, #<Mime::Type:0x00000003c4b960 @synonyms=[], @symbol=:url_encoded_form, @string="application/x-www-form-urlencoded">]
Billy Logan
  • 2,470
  • 6
  • 27
  • 45

1 Answers1

2

I haven't seen this syntax before

def create
  respond_to current_user.tasks.create(task_params)
end

It would usually be something like

def create
  if current_user.tasks.create(task_params)
    @created = true
  end
  respond_to do |format|
    format.html #will do default which is render :action => "create"
  end
end

I think this might be your problem: respond_to is being passed a Task object which it doesn't expect, and i guess calling upcase on what it's passed is part of it's normal behaviour.

EDIT Is it perhaps meant to be respond_with rather than respond_to?

http://apidock.com/rails/ActionController/MimeResponds/respond_with

Max Williams
  • 32,435
  • 31
  • 130
  • 197
  • agreed, it seems like `respond_to` needs `mimes` and it calls a class called `Collector` and upcase the mimes. that could be the reason. https://github.com/rails/rails/blob/6515e953f4184014cfd50c3d571d394d0e95c367/actionpack/lib/action_controller/metal/mime_responds.rb#L253 – sameera207 Aug 24 '15 at 23:38
  • @BillyLogan since you've posted another question basically reiterating this one but with my solution, http://stackoverflow.com/questions/32206401/whats-the-difference-between-respond-to-and-respond-with-in-rails, perhaps you should mark my answer as correct? – Max Williams Aug 25 '15 at 14:50
  • In my case, the edit was spot on. I was using `respond_to` when I meant to use `respond_with`. – taylorthurlow Oct 22 '17 at 01:43