Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.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 on rails 格式错误的json_Ruby On Rails_Json_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 格式错误的json

Ruby on rails 格式错误的json,ruby-on-rails,json,ruby-on-rails-4,Ruby On Rails,Json,Ruby On Rails 4,我的应用程序中有一个模型事件,我有两个问题,第一个问题是返回的json与请求一起出现问题: $ curl http://0.0.0.0:3000/events 它给了我 { "events":[ {"events":{"name":"First"}}, {"events":{"name":"Second"}}], "meta":{"total_pages":9, "next_pa

我的应用程序中有一个模型事件,我有两个问题,第一个问题是返回的json与请求一起出现问题:

$ curl http://0.0.0.0:3000/events
它给了我

{
  "events":[
            {"events":{"name":"First"}},
            {"events":{"name":"Second"}}],
            "meta":{"total_pages":9,
                    "next_page":"http://0.0.0.0:3000/events?page=2"}
}    
我认为它应该给出

{
      "events":[
                  {"name":"First" },
                  {"name":"Second"}
               ],
               "meta":{
                       "total_pages":9,
                       "next_page":"http://0.0.0.0:3000/events?page=2"
                      }
 }    
此外,我正在使用gems-draper和active_model_序列化程序,第二个问题是json格式不符合序列化程序定义:

class EventSerializer < ActiveModel::Serializer
   attributes :id, :date_end
 end
class EventSerializer
我想知道我应该在哪里解决这些问题

回答问题时,生成json的代码:

class EventsController < ApplicationController

def index
    @events = Event.order(:name).page(params[:page])      
    @events = @events.where(id: params[:ids].split(',')) if params[:ids].present?

    render json: @events.decorate, meta: { total_pages: @events.total_pages, next_page: next_page_url(@events)}

end

def next_page_url(event)
   unless event.last_page?
      next_page = event.current_page + 1 unless event.last_page?

      events_url(params.merge({:page => next_page}))
   end
end
类事件控制器next_page}))
结束
结束
还有装饰师:

class EventDecorator < Draper::Decorator
delegate :current_page, :total_pages, :limit_value


  def name
    object.name
  end

 end
class EventDecorator
生成json的代码在哪里?^秒。这很可能就是问题中的错误所在。我只是在问题中添加了更多代码,以显示json是在哪里生成的。