[英]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 当我向控制器发送数据时,出现以下错误
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?
在Rails中,response_to和response_with有什么区别?
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
当我使用
Undefined method upcase for Task
它说Undefined method upcase for Task
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. 由于
respond_with current_user.tasks.create(task_params)
将生成html
响应。
In your routes.rb change 在你的routes.rb改变
resources :tasks
to 至
resources :tasks, :defaults => {:format => "json"}
Try this one: 试试这个:
def create
respond_with(current_user.tasks.create(task_params), :location => tasks_url)
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.