Rails:调整通过 jbuilder 服务的单个对象的属性
Rails: Tweaking attribute of single object served through jbuilder
我正在制作一个 API 以 JSON 格式提供周日历(周一至周日)。现在每个星期日历都有属性'name'(字符串),'start_date'(时间对象,指的是日历开始的星期一)。我正在尝试使用 Rails.
附带的 jbuilder gem
问题:
1) Calendars GET users/user_id/calendars/calendar_id serves JSON with information about the calendar
Failure/Error: expect_json({name: calendar.name, start_date: Date.new(2015, 3, 2).strftime('%Y%m%d')})
expected: "20150302"
got: "2015-03-02T00:00:00.000Z"
(compared using ==)
# ./spec/requests/calendars_spec.rb:14:in `block (3 levels) in <top (required)>'
我想稍微调整 start_date 的输出格式,因为这样在前端处理起来会更容易。我知道该怎么做(Time.now.strformat(%y%m%d) 或其他),但我不知道如何在 jbuilder 的上下文中进行。这是我的 jbuilder 文件中的内容:
json.extract! @calendar, :name, :start_date
我尝试了构建器文档中的很多语法,但它们似乎都适用于我提供 JSON 数组的情况。在这种情况下,我尝试提供单个模型的 JSON 表示。
我关于 Whosebug 的第一个问题,所以我希望这个问题比较清楚。检查期望以了解我想要什么。
JBuilder 不要求您只使用 extract!()
。您的 .jbuilder 文件可能如下所示:
json.name @calendar.name
json.start_date @calendar.start_date.strftime('%Y-%d-%m')
该语法在单独的行中指定您想要在 json 中的每个 name/value 对。
In this instance I'm trying to serve a JSON representation of a single
model.
上面的输出是:
{"name":"hello","start_date":"2000-01-01"}
请注意,rails 列类型 :time
没有在数据库中存储正确的日期信息 table--rails 使用了一个虚拟日期 2000-01-01
.毕竟,你说你只想存储一个时间!因为您也对日期感兴趣,所以您需要使用不同的列类型。
一个测试:
spec/requests/calendars_spec.rb:
require 'spec_helper'
describe "json API" do
describe "GET calendars/1.json" do
let(:calendar) { FactoryGirl.create(:week_calendar) } #Create a WeekCalendar in the test db, and assign it to the variable calendar.
it "returns the correct json" do
test_calendar = {
name: calendar.name,
start_date: calendar.start_date.strftime("%Y-%m-%d"),
}
visit '/calendars/1.json'
expect(page.body).to eq(test_calendar.to_json)
end
end
end
spec/factories.rb:
FactoryGirl.define do
factory :week_calendar do
name "test"
start_date DateTime.new(2015, 2, 26)
end
end
宝石文件:
group :test do
gem 'selenium-webdriver', '2.35.1'
gem 'capybara', '2.1.0'
gem "factory_girl_rails", "4.2.0"
end
app/controllers/calendars_controller:
class CalendarsController < ApplicationController
def show
@calendar = WeekCalendar.find(params[:calendar_id])
respond_to :json
end
end
db/migrations/20150228082528_create_week_calendars.rb:
class CreateWeekCalendars < ActiveRecord::Migration
def change
create_table :week_calendars do |t|
t.string :name
t.datetime :start_date
t.timestamps
end
end
end
config/routes.rb:
get "calendars/(:calendar_id)", to: "calendars#show"
我正在制作一个 API 以 JSON 格式提供周日历(周一至周日)。现在每个星期日历都有属性'name'(字符串),'start_date'(时间对象,指的是日历开始的星期一)。我正在尝试使用 Rails.
附带的 jbuilder gem问题:
1) Calendars GET users/user_id/calendars/calendar_id serves JSON with information about the calendar
Failure/Error: expect_json({name: calendar.name, start_date: Date.new(2015, 3, 2).strftime('%Y%m%d')})
expected: "20150302"
got: "2015-03-02T00:00:00.000Z"
(compared using ==)
# ./spec/requests/calendars_spec.rb:14:in `block (3 levels) in <top (required)>'
我想稍微调整 start_date 的输出格式,因为这样在前端处理起来会更容易。我知道该怎么做(Time.now.strformat(%y%m%d) 或其他),但我不知道如何在 jbuilder 的上下文中进行。这是我的 jbuilder 文件中的内容:
json.extract! @calendar, :name, :start_date
我尝试了构建器文档中的很多语法,但它们似乎都适用于我提供 JSON 数组的情况。在这种情况下,我尝试提供单个模型的 JSON 表示。
我关于 Whosebug 的第一个问题,所以我希望这个问题比较清楚。检查期望以了解我想要什么。
JBuilder 不要求您只使用 extract!()
。您的 .jbuilder 文件可能如下所示:
json.name @calendar.name
json.start_date @calendar.start_date.strftime('%Y-%d-%m')
该语法在单独的行中指定您想要在 json 中的每个 name/value 对。
In this instance I'm trying to serve a JSON representation of a single model.
上面的输出是:
{"name":"hello","start_date":"2000-01-01"}
请注意,rails 列类型 :time
没有在数据库中存储正确的日期信息 table--rails 使用了一个虚拟日期 2000-01-01
.毕竟,你说你只想存储一个时间!因为您也对日期感兴趣,所以您需要使用不同的列类型。
一个测试:
spec/requests/calendars_spec.rb:
require 'spec_helper'
describe "json API" do
describe "GET calendars/1.json" do
let(:calendar) { FactoryGirl.create(:week_calendar) } #Create a WeekCalendar in the test db, and assign it to the variable calendar.
it "returns the correct json" do
test_calendar = {
name: calendar.name,
start_date: calendar.start_date.strftime("%Y-%m-%d"),
}
visit '/calendars/1.json'
expect(page.body).to eq(test_calendar.to_json)
end
end
end
spec/factories.rb:
FactoryGirl.define do
factory :week_calendar do
name "test"
start_date DateTime.new(2015, 2, 26)
end
end
宝石文件:
group :test do
gem 'selenium-webdriver', '2.35.1'
gem 'capybara', '2.1.0'
gem "factory_girl_rails", "4.2.0"
end
app/controllers/calendars_controller:
class CalendarsController < ApplicationController
def show
@calendar = WeekCalendar.find(params[:calendar_id])
respond_to :json
end
end
db/migrations/20150228082528_create_week_calendars.rb:
class CreateWeekCalendars < ActiveRecord::Migration
def change
create_table :week_calendars do |t|
t.string :name
t.datetime :start_date
t.timestamps
end
end
end
config/routes.rb:
get "calendars/(:calendar_id)", to: "calendars#show"