Rails应用程序将太多无关数据发送到json

时间:2019-01-31 10:16:53

标签: ruby-on-rails json

所需的输出:仅来自事件的数据,例如:

[{"id":600,"title":600,"start":"2018-10-17T15:24:06.000Z","end":"2018-10-17T17:11:06.000Z","color":"green"}
[{"id":601,"title":601,"start":"2019-10-17T15:24:06.000Z","end":"2018-10-17T17:11:06.000Z","color":"green"}

由于某种原因,当前输出还会提供来自application.html.erbapplication.js以及其他文件的信息: enter image description here

events / _event.json.jbuilder:

json.id event.id
json.title event.id
json.start event.starts_at
json.end event.ends_at
json.color event.status_color unless event.status_color.blank?
json.url event_url(event, format: :json)

index.json.jbuilder:

json.array! @events, partial: 'events/event', as: :event

Contoller:

class EventsController < ApplicationController
  def index
    @events = Event.all
    #@q = Event.ransack(params[:q])
    #@events = @q.result.includes(:location, :client, :jobs).paginate(:page => params[:page], :per_page => 15).order("created_at DESC")
  end end

控制台:

Started GET "/events.json" for 143.27.126.62 at 2019-01-31 10:49:24 +0000
Cannot render console from 143.27.126.62! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by EventsController#index as JSON
  User Load (0.8ms)  SELECT  "users".* FROM "users" WHERE (users.tenant_id IS NULL) AND "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ /usr/local/rvm/gems/ruby-2.5.3/bundler/gems/milia-4c36b46f3f42/lib/milia/control.rb:102
  Tenant Load (0.7ms)  SELECT "tenants".* FROM "tenants" INNER JOIN "tenants_users" ON "tenants"."id" = "tenants_users"."tenant_id" WHERE (tenants.tenant_id IS NULL) AND "tenants_users"."user_id" = $1  [["user_id", 1]]
  ↳ /usr/local/rvm/gems/ruby-2.5.3/bundler/gems/milia-4c36b46f3f42/lib/milia/control.rb:69
MILIA >>>>> [change tenant] new: 1      old: 1
MILIA >>>>> [auth_tenant callback]
  Tenant Load (0.6ms)  SELECT  "tenants".* FROM "tenants" WHERE (tenants.tenant_id IS NULL) AND "tenants"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ /usr/local/rvm/gems/ruby-2.5.3/bundler/gems/milia-4c36b46f3f42/lib/milia/base.rb:126
  CACHE Tenant Load (0.0ms)  SELECT  "tenants".* FROM "tenants" WHERE (tenants.tenant_id IS NULL) AND "tenants"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ /usr/local/rvm/gems/ruby-2.5.3/bundler/gems/milia-4c36b46f3f42/lib/milia/base.rb:126
  CACHE Tenant Load (0.0ms)  SELECT  "tenants".* FROM "tenants" WHERE (tenants.tenant_id IS NULL) AND "tenants"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ /usr/local/rvm/gems/ruby-2.5.3/bundler/gems/milia-4c36b46f3f42/lib/milia/base.rb:126
  Rendering events/index.json.jbuilder within layouts/application
  Event Load (80.7ms)  SELECT "events".* FROM "events" WHERE (events.tenant_id = 1)
  ↳ app/views/events/index.json.jbuilder:1
  Rendered events/_event.json.jbuilder (1.1ms)
  Rendered events/_event.json.jbuilder (0.3ms)
  Rendered events/_event.json.jbuilder (0.2ms)
  Rendered events/_event.json.jbuilder (0.2ms)
  Rendered events/_event.json.jbuilder (0.3ms)

也许与Rendering events/index.json.jbuilder within layouts/application有关?

2 个答案:

答案 0 :(得分:1)

因为我需要编写一些代码,所以现在将其发布为答案:

您确定它正在执行/events.json吗?还是在做/events? (查看您的屏幕截图,我认为您只是在Chrome地址栏中输入了/events.json,因为默认情况下(应来自AFAIK)其中应包含“主机名”。

rails如何通过this doc确定要呈现的响应格式:

示例:

GET /posts/5.xml   | request.format => Mime::XML
GET /posts/5.xhtml | request.format => Mime::HTML
GET /posts/5       | request.format => Mime::HTML or MIME::JS, or request.accepts.first`

因此

  • GET /events.xml将“默认”呈现app/views/events.xml.TEMPLATE_ENGINE

  • GET /events.json将“默认”呈现app/views/events.json.TEMPLATE_ENGINE

  • GET /events将默认从“ Accept”标头中的第一个值推断出来。也就是说,如果我在Chrome地址栏中输入此URL,然后在Chrome网络标签中查看,Chrome似乎会生成“接受”标头,如下所示:

    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
    

    ...的第一个值为text/html,这就是Rails默认情况下将呈现app/views/posts.html.TEMPLATE_ENGINE的原因。

    ^目前,这是我的猜测,这是为什么您看到HTML的原因,因为您正在执行的请求可能是GET /events,而不是正确的请求GET /events.json。不过,您仍然可以执行GET /events,但是您需要将“ Accept”标头修改为等于"application/json"

调试:

要在上面测试我的理论,是否可以打开Chrome“网络标签”(如果不知道如何打开,请单击HERE)。然后正常向Rails端点发送/发送请求(不知道您是否正在单击按钮以发送请求?)。然后,将填充“网络”选项卡。您会看到类似以下的内容: Network Request Example Screenshot

如您所见,URL是posts.json,它应该是它。如果您看到的是events而不是events.json,那么这就是您的问题;您需要确保它正在执行events.json,或者仍然可以执行events,但是更新请求中的“ Accept”标头(如您在屏幕快照中所看到的,该值应为"Accept: application/json"

P.S。我在上面所说的所有情况都是在您未在控制器操作中“手动”渲染并且您未手动调用render的情况下进行的(这将执行Rails默认设置,而Rails将隐式渲染为我已经在上面的答案中描述过)

答案 1 :(得分:0)

尝试在操作方法的最后一行添加respont_to :json

class EventsController < ApplicationController
  def index
    @events = Event.all
    resond_to :json # add this line
  end
end