我是ruby on rails的新手,我有一个问题是获取datetime类型的参数并将它们传递给编辑页面。这个想法是有一个名为week的表,其中包含有关课程的信息以及每门课程的开始周(日期),用户可以创建和编辑这些周。我有一个用于编辑的表单页面,并创建课程的名称和开始日期。我使用f.datetime_select从用户获取每个课程的开始日期。这是表单和索引页面:
索引(第一页):
%tbody
- @weeks.each do |week|
%tr
%td= week.course ? week.course.name : "unassigned"
%td= week.start.strftime("%a, %d.%m.%Y").to_s
%td= link_to 'Edit', edit_week_path(week)
%td= link_to 'Destroy', week, :method => :delete, :data => { :confirm => 'Are you sure?' }
表单页面:
= form_for @week do |f|
- if @week.errors.any?
#error_explanation
%h2= "#{pluralize(@week.errors.count, "error")} prohibited this week from being saved:"
%ul
- @week.errors.full_messages.each do |msg|
%li= msg
.form-group
= f.label :course
= @course.name
.form-group.form-inline
= f.label :start
= f.datetime_select :start, {order: [:month, :day, :year], prompt: { day: 'Select day', month: 'Select month', year: 'Select year' }}, {required: true}
.actions
= f.submit 'Save', :class => "btn btn-primary"
创建新日期没有问题,但我在编辑用户在上述表单中保存的日期时遇到问题。实际上当用户点击编辑页面时,日期时间错误显示如下:
F, [2018-01-25T21:16:43.454087 #819] FATAL -- :
ActionView::Template::Error (undefined method `to_datetime' for 0:Fixnum):
11: = @course.name
12: .form-group.form-inline
13: = f.label :start
14: = f.datetime_select :start, {order: [:month, :day, :year], prompt: { day: 'Select day', month: 'Select month', year: 'Select year' }}, {required: true}
15: //= f.date_select :start, {}, { :class => "form-control" }
16: .actions
17: = f.submit 'Save', :class => "btn btn-primary"
app/views/weeks/_form.html.haml:14:in `block in _app_views_weeks__form_html_haml___241486880657666967_43852860'
app/views/weeks/_form.html.haml:1:in `_app_views_weeks__form_html_haml___241486880657666967_43852860'
app/views/weeks/edit.html.haml:3:in `_app_views_weeks_edit_html_haml___597540861062441854_41928920'
这是控制器文件:
class WeeksController < ApplicationController
before_filter :authenticate_user!
before_filter :admincheck
before_action :set_week, only: [:show, :edit, :update, :destroy]
# GET /weeks
# GET /weeks.json
def index
@weeks = @course.weeks
end
# GET /weeks/new
def new
@week = Week.new
end
# GET /weeks/1/edit
def edit
end
def show
end
# POST /weeks
# POST /weeks.json
def create
@week = Week.new(week_params.merge(course_id: @course.id))
if @week.save
redirect_to '/weeks'
else
render action: 'new'
end
end
# PATCH/PUT /weeks/1
# PATCH/PUT /weeks/1.json
def update
if @week.update(week_params)
redirect_to '/weeks'
else
render action: 'edit'
end
end
# DELETE /weeks/1
# DELETE /weeks/1.json
def destroy
@week.destroy
redirect_to '/weeks'
end
private
# Use callbacks to share common setup or constraints between actions.
def set_week
@week = Week.find(params[:id])
end
list through.
def week_params
params.require(:week).permit(:start,:course_id)
end
end
这是schema.rb中表的定义:
create_table "weeks", force: true do |t|
t.datetime "start"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "course_id"
end
任何帮助将不胜感激:)
答案 0 :(得分:0)
如果这是(或曾经)是4.0.0
和4.0.3
之间的轨道:this is a known bug就是was fixed in 4.0.4。
因此,在这种情况下,最好的解决方案就是升级导轨。