2

When I'm sending data to my controller I'm getting the following error

enter image description here

with the parameters

{"title"=>"some",
 "user_id"=>"2",
 "task"=>{"title"=>"some"}}

Why is that so? And what's the difference between respond_to and respond_with in Rails?

class TasksController < ApplicationController
  respond_to :json

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

  private

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

end

When I'm using respond_to it says Undefined method upcase for Task

Billy Logan
  • 2,470
  • 6
  • 27
  • 45

2 Answers2

2

It's saying it doesn't recognize the format of your response. Since respond_with current_user.tasks.create(task_params) will generate a html response.

In your routes.rb change

resources :tasks

to

resources :tasks, :defaults => {:format => "json"}

This question may help you

Community
  • 1
  • 1
thedanotto
  • 6,895
  • 5
  • 45
  • 43
1

Try this one:

def create
  respond_with(current_user.tasks.create(task_params), :location => tasks_url)
end