gpt4 book ai didi

ruby-on-rails - RoR form_for : Hidden field isn't being included within the object parameter and therefore doesn't work.

转载 作者:行者123 更新时间:2023-12-03 18:07:19 24 4
gpt4 key购买 nike

我正在使用表单添加条目,我需要将当前用户的 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 被保存为 null。我注意到在下面的控制台输出中存在 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]

最佳答案

其他答案是为 Rails 2 编写的。 Salil 答案的 Rails 3 改编使用这种格式:

<%= f.hidden_field :user_id, :value => current_user.id %>

关于ruby-on-rails - RoR form_for : Hidden field isn't being included within the object parameter and therefore doesn't work.,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3211619/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com