【发布时间】:2020-08-12 21:42:01
【问题描述】:
我正在尝试获取数据库表中新创建行的 ID,以便以后可以使用它。但它不会通过它只停留在查看器中。关闭选项卡或窗口后,我需要该 ID。我知道可能有其他方法可以找到该行的 id,但由于一些更改,网站需要使用 id 来完成。
当查看器被访问时,它会通过一些 ruby on rails 语句。
<% if user_signed_in? %>
<% @att_record = Attendance.new(:user_id => current_user.id, :time_in => Time.now) %>
<% #@attendance_log = @att_record #tried this already, controller doesn't receive it %>
<%= hidden_field_tag :@get_int, :value => @att_record.id %>
<% @att_record.save %>
有人建议我使用 hidden_field_tag 来获取整数,但我无法让它工作。
一旦窗口关闭,它应该能够使用整数。关闭窗口后,将执行以下函数。
def unload
@get_int
@attendance_log = Attendance.find_by(id: get_int)
@attendance_log.time_out = Time.now
@attendance_log.save
head :ok
end
@att_record 是在视图中创建的,而 get_int 和出席日志是在卸载函数中创建的。
【问题讨论】:
-
试试stackoverflow.com/questions/13672155/…。您可以在 SO 上找到更多信息。
标签: ruby-on-rails ruby view controller hidden-field