最近,我连接了pickadate gem。现在我的_form
样子是这样的:
<%= form_for(@event, :html => {:class => 'form-horizontal'}) do |f| %>
<%= render('error_message') %>
<div class="field form-group">
<%= f.label :name %>
<%= f.text_field :name, class: "form-control" %>
</div>
<div class="field form-group">
<%= f.label :start_time %>
<%= f.text_field :start_time, class: "datepicker form-control"%>
</div>
<div class="actions">
<%= f.submit 'Save', class: "btn btn-success" %>
</div>
<% end %>
<script type="text/javascript">
$('.datepicker').pickadate({
//customization
})
</script>
这里是new.html.erb
:
<% provide(:title, t('new_event_title')) %>
<h1><%= t('new_event_h1') %></h1>
<%= render 'form' %>
如您所见,Event
模型有 2 个参数:(name
字符串类型)和start_time
(日期时间类型)。这是我的create
方法和event_params
:
def create
@event = Event.new(event_params)
redirect_to events_path
end
def event_params
params.require(:event).permit(:name, :start_time)
end
但是,通过单击“保存”按钮,我收到以下错误:
ActionController::ParameterMissing in EventsController#create
param is missing or the value is empty: event
这是服务器日志:
Started POST "/events" for ::1 at 2016-03-07 15:23:08 +0300
Processing by EventsController#create as HTML
Can't verify CSRF token authenticity
{"controller"=>"events", "action"=>"create"}
Completed 400 Bad Request in 2ms (ActiveRecord: 0.0ms)
我渲染的_form:
<form class="form-horizontal" id="new_event" action="/events" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓" /><input type="hidden" name="authenticity_token" value="jvg+<...>Q==" />
<div class="field form-group">
<label for="event_name">Name</label><br>
<input class="form-control" type="text" name="event[name]" id="event_name" />
</div>
<div class="field form-group">
<label for="event_start_time">Start time</label><br>
<input class="datepicker form-control" type="text" name="event[start_time]" id="event_start_time" />
</div>
<div class="actions">
<input type="submit" name="commit" value="Save" class="btn btn-success" />
</div>
</form>
我猜表格有问题。有任何想法吗?提前致谢!