Billy Logan
Billy Logan

Reputation: 2520

What's the difference between respond_to and respond_with in Rails?

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

Upvotes: 2

Views: 746

Answers (2)

thedanotto
thedanotto

Reputation: 7307

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

Upvotes: 2

user5245636
user5245636

Reputation:

Try this one:

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

Upvotes: 1

Related Questions