What's the difference between respond_to and r

2019-05-28 18:12发布

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

2条回答
Bombasti
2楼-- · 2019-05-28 18:28

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

查看更多
Lonely孤独者°
3楼-- · 2019-05-28 18:37

Try this one:

def create
  respond_with(current_user.tasks.create(task_params), :location => tasks_url)
end
查看更多
登录 后发表回答