我正在尝试使用ruby论坛助手设置日期时间字段。
<%= form_for @event do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<strong>Title</strong>
<div class="field">
<%= f.text_field :title %>
</div>
<strong>Description</strong>
<div class="field">
<%= f.text_area :description, :class => "comment" %>
</div>
<strong>Location</strong>
<div class="field">
<%= f.text_field :location %>
</div>
<strong>Time</strong>
<div class="field">
<%= select_datetime Date.today, :prefix => :start_date %>
</div>
<div class="actions">
<%= f.submit "Add Event" %>
</div>
<% end %>
在控制器中我这样做: class EventsController&lt; ApplicationController的 def指数 @event = Event.new 端
def create
@event = current_user.events.build(params[:event])
if @event.save
redirect_to root_path, :flash => { :success => "#{@event.inspect}!" }
else
@feed_items = []
render 'pages/home'
end
end
end
然而日期时间从未设定......我做错了什么?
答案 0 :(得分:0)
您需要处理控制器中的内容,例如:
def create
@event = current_user.events.build(params[:event])
datetime=DateTime.civil(params[:start_date][:year].to_i, params[:start_date][:month].to_i, params[:start_date][:day].to_i,
params[:start_date][:hours].to_i,params[:start_date][:minutes].to_i, params[:start_date][:seconds].to_i)
@event.datetime = datetime
if @event.save
redirect_to root_path, :flash => { :success => "#{@event.inspect}!" }
else
@feed_items = []
render 'pages/home'
end
end
答案 1 :(得分:0)
这个问题差不多已经过了五年了,但为了防止将来有人帮助,有一种更简单的方法来创建具有datetime属性的模型对象。
将<%= select_datetime Date.today, :prefix => :start_date %>
替换为<%= f.datetime_select :datetime %>
并在控制器中定义创建操作,如下所示。
def create
@event = current_user.events.build(params[:event])
if @event.save
redirect_to root_path, :flash => { :success => "#{@event.inspect}!" }
else
@feed_items = []
render 'pages/home'
end
end