【发布时间】:2026-02-06 04:40:01
【问题描述】:
我有一个带有 date_select 的表单。
<%= form_for @post, url: { controller: "posts", action: "create" } do |form| %>
<%= form.date_select :date %>
我正在尝试根据 post.date 和 post.user_id 字段检查我的 post_controller.rb 是否存在以下行
@existing_post = Post.where(user_id: current_user.id, date: params[:post][:date].to_s).first
if @existing_post.present?
@existing_post.body += "<hr>#{params[:post][:body]}"
@existing_post.save(post_params)
else
@post = Post.new(post_params)
end
如果帖子存在,我想使用 += 附加,如果帖子不存在,我想创建新的。但 params[:post][:date].to_s 不返回任何数据。它看起来 NIL。我错过了什么?
后加载(1.8 毫秒)选择“posts”。 FROM “posts”,其中“posts”。“user_id” = $1 AND “posts”。“date”为 NULL ORDER BY "posts"."id" ASC LIMIT $2 [["user_id", 1], ["LIMIT", 1]]*
【问题讨论】:
标签: ruby-on-rails postgresql forms date