首先
post的json中的时间格式是RTC,比如1487087379
我现在是这么做的
def create
@game = Game.new(game_params)
@game.update!(time_of_begin: Time.at(game_params[:time_of_begin]))
@game.update!(time_of_end: Time.at(game_params[:time_of_end]))
respond_to do |format|
if @game.save
format.html { redirect_to @game, notice: 'Game was successfully created.' }
format.json { render :show, status: :created, location: @game }
else
format.html { render :new }
format.json { render json: @game.errors, status: :unprocessable_entity }
end
end
end
def game_params
params.require(:game).permit(:number_of_player, :theme_id, :time_of_begin, :time_of_end)
end
但是感觉有点累赘,有什么比较简洁的写法吗