Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby Rails 5.2 ActionController::PatientController中的未知信息#索引错误_Ruby_Ruby On Rails 5 - Fatal编程技术网

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