Ruby on rails RubyonRails:在datetime范围内呈现时间表的JSON?

Ruby on rails RubyonRails:在datetime范围内呈现时间表的JSON?,ruby-on-rails,ruby,json,rest,Ruby On Rails,Ruby,Json,Rest,我是RubyonRails新手,正在尝试为类的员工时间表服务编写RESTAPI。我无法根据日期选择时间表并发回json 这是我的密码: before_filter only: :index do parse_request unless @json.has_key?('timesheet') && @json['timesheet']['year'] && @json['timesheet']['month'] render nothing: true, st

我是RubyonRails新手,正在尝试为类的员工时间表服务编写RESTAPI。我无法根据日期选择时间表并发回json

这是我的密码:

before_filter only: :index do
parse_request
unless @json.has_key?('timesheet') && @json['timesheet']['year'] && @json['timesheet']['month']
  render nothing: true, status: :bad_request
end
end

def index
year = @json['timesheet']['year']
month = @json['timesheet']['month']
if(@json.has_key?(:day))
  week = @json['timesheet']['day']
  begin_date = DateTime.new(year, month, day, 00, 00, 00)
  end_date = begin_date + 1.week + 1.day
else
  begin_date = DateTime.new(year, month, 01, 00, 00, 00)
  end_date = begin_date + 1.month + 1.day
end
if(params.has_key?(:employer_id))
  render json: Timesheet.where(employer_id: params[:employer_id], time: begin_date..end_date)
elsif(params.has_key?(:employee_id))
  render json: Timesheet.where(employee_id: params[:employee_id], time: begin_date..end_date)
else
  render nothing: true, status: :bad_request
end
以下是我的相关路线:

scope '/:employer_id' do
          get '/' => 'api_employers#show'
          put '/' => 'api_employers#update'
            scope '/timesheets' do
              put '/' => 'api_timesheets#index'
            end

我尝试在终端中使用以下命令获取时间表:

curl -H "Content-Type: application/json" -X PUT -d '{"timesheet":{"employer_id":"1", "employee_id":"1", "year":"2016", "month":"3"}' http://tester-1234.herokuapp.com/api/v1/employees/1/timesheets

但它什么都没有。我在索引中检查day参数的原因是,如果用户指定了一天,它应该返回该天起一周的时间表,如果用户没有指定,它应该只返回整个月的时间表。非常感谢您的帮助

我会在控制器中放置一个窥探器,看看代码是否按照预期完成了操作@PetrGazarov:我不可能做一个简单的“打印”语句或类似的事情吗?我不认为有一种简单的方法可以将某些内容打印回终端,因为这样你就必须发送响应,这就是你首先遇到的问题。这就是说,如果您正在测试JSON响应,请尝试这个方便的工具:可能重复的
curl -H "Content-Type: application/json" -X PUT -d '{"timesheet":{"employer_id":"1", "employee_id":"1", "year":"2016", "month":"3"}' http://tester-1234.herokuapp.com/api/v1/employees/1/timesheets