![](/img/trans.png)
[英]Ruby on rails: What's the difference between respond_to and respond_with?
[英]What's the difference between respond_to and respond_with in Rails?
当我向控制器发送数据时,出现以下错误
与参数
{"title"=>"some",
"user_id"=>"2",
"task"=>{"title"=>"some"}}
为什么会这样? 在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
当我使用Undefined method upcase for Task
它说Undefined method upcase for Task
就是说它无法识别您的回复格式。 由于respond_with current_user.tasks.create(task_params)
将生成html
响应。
在你的routes.rb改变
resources :tasks
至
resources :tasks, :defaults => {:format => "json"}
这个问题可能对您有帮助
试试这个:
def create
respond_with(current_user.tasks.create(task_params), :location => tasks_url)
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.