Ruby Rails 5.2 ActionController::PatientController中的未知信息#索引错误
它起作用了 您在错误消息Ruby Rails 5.2 ActionController::PatientController中的未知信息#索引错误,ruby,ruby-on-rails-5,Ruby,Ruby On Rails 5,它起作用了 您在错误消息ActionController::UnknownFormat中看到的信息告诉您出了什么问题 正如您所提到的,request.format返回:html,但您只定义了format.json 看起来您没有正确格式化JSON请求。为了做到这一点,您应该为JSON请求定义标题,或者请求以.JSON结尾的url(rails的一个特性) JSON请求头: def index respond_to do |format| format.json do
ActionController::UnknownFormat
中看到的信息告诉您出了什么问题
正如您所提到的,request.format
返回:html
,但您只定义了format.json
看起来您没有正确格式化JSON请求。为了做到这一点,您应该为JSON请求定义标题,或者请求以.JSON
结尾的url(rails的一个特性)
JSON请求头:
def index
respond_to do |format|
format.json do
render json: Patient.all
end
format.html do
render :index
end
end
end
您还没有为“html”请求指定响应,因此这样做会导致错误。如果要访问“/patients.json”,您应该会收到响应。如果这应该默认为json请求,那么在routes@enginersmnky中指定格式。谢谢,这就是问题所在!它正在工作
def index
respond_to do |format|
format.json do
render json: Patient.all
end
format.html do
render :index
end
end
end
Content-Type: application/json
Accept: application/json