在我的帖子视图中,我有一个_form
带有title
和releaseDate
参数的。
在我的控制器中,我从 中获取值params[:post][:title]
,但params[:post][:releaseDate]
是空的。
post_form
<%= form_for(@post) do |f| %>
<% if @post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :releaseDate %><br />
<%= f.datetime_select :releaseDate %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
post_controller
def create
@title = params[:post][:title]
@date = params[:post][:releaseDate]
@post = Post.new(title: @title, releaseDate: @date)
@post.user_id = current_user.id
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render json: @post, status: :created, location: @post }
else
format.html { render action: "new" }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
(我没有使用@post = Post.new(params[:post])
,因为我想对控制器中的title
and做一些事情。)releaseDate
post.rb (模型)
class Post < ActiveRecord::Base
attr_accessible :releaseDate, :title, :user_id
belongs_to :user
end
关于如何从表单中获取日期时间值并进入我的控制器的任何建议?