我正在使用表单添加条目,并且需要将当前用户的 id 与条目参数一起发送。这是我的表单代码:
<% form_for(@entry) do |f| %>
<%= f.error_messages %>
<%= hidden_field_tag 'user_id', current_user.id %>
<p>
<%= f.label :date %><br />
<%= f.date_select :date %>
</p>
<p>
<%= f.label :note %><br />
<%= f.text_field :note %>
</p>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
问题是 user_id 被保存为空。我在下面的控制台输出中注意到 user_id 存在,但它实际上并不位于条目对象参数中。我怎样才能解决这个问题?谢谢阅读。
Processing EntriesController#create (for 127.0.0.1 at 2010-07-09 19:57:55) [POST]
Parameters: {"commit"=>"Create", "action"=>"create", "user_id"=>"3", "entry"=>{"date(1i)"=>"2010", "date(2i)"=>"7", "date(3i)"=>"9", "note"=>"bb"}, "controller"=>"entries"}
Entry Create (0.4ms) INSERT INTO "entries" ("entry_id", "created_at", "updated_at", "date", "user_id", "note") VALUES(NULL, '2010-07-09 09:57:55', '2010-07-09 09:57:55', '2010-07-09', NULL, 'bb')
Redirected to http://localhost:3000/entries/7
Completed in 24ms (DB: 0) | 302 Found [http://localhost/entries]