Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 提交Rails 4中所有表单的设计用户名_Ruby On Rails_Ruby On Rails 4_Devise_Controller - Fatal编程技术网

Ruby on rails 提交Rails 4中所有表单的设计用户名

Ruby on rails 提交Rails 4中所有表单的设计用户名,ruby-on-rails,ruby-on-rails-4,devise,controller,Ruby On Rails,Ruby On Rails 4,Devise,Controller,我有一个Rails 4应用程序,在一个遗留的PostgreSQL数据库上使用Desive、CanCan和Bootstrap。几乎我所有的表都有一个“moduser”列和我的modtime,并在列中创建了_(如前所述,db是遗留的,因此与Rails约定有偏差) 目的是记录最后一个修改记录的用户的用户名(可以为空)。对于所有具有moduser列的模型(表),我希望表单自动提交设计用户名(即current_user.username)以及其他参数,而无需用户在文本字段中输入:value=>curren

我有一个Rails 4应用程序,在一个遗留的PostgreSQL数据库上使用Desive、CanCan和Bootstrap。几乎我所有的表都有一个“moduser”列和我的modtime,并在列中创建了_(如前所述,db是遗留的,因此与Rails约定有偏差)

目的是记录最后一个修改记录的用户的用户名(可以为空)。对于所有具有moduser列的模型(表),我希望表单自动提交设计用户名(即current_user.username)以及其他参数,而无需用户在文本字段中输入
:value=>current_user.username

我也许可以在每个_form.html.erb中为moduser使用
input type=“hidden”
元素,但我觉得这并不是很枯燥

如何以一种简单的方式实现它,以便每个表单提交当前的_user.username并将其映射到表的moduser列(如果存在)

更新

我喜欢控制器的想法,所以我试着实现它,但不幸的是,我被困在了如何将各个部分组合在一起。我担心的是:

module ModUpdateable extend ActiveSupport::Concern

  included do
   after_update :update_moduser
   after_create :update_moduser
  end

  def update_moduser
    update(moduser: current_user.username)
  end
end
我只是在我的CountriesController上尝试一下,所以我将
include ModUpdateable
添加到控制器代码的顶部,就在我的
过滤器之前:验证用户

当我启动puma服务器时,会出现路由错误

CountriesController:Class的“更新”后未定义的方法

app/controllers/concerns/mod_updateable.rb:4:in
block in' app/controllers/countries\u controller.rb:2:in
include'
app/controllers/countries\u controller.rb:2:in
' app/controllers/countries\u controller.rb:1:in`

我错过什么了吗


我还怀疑
update\u moduser
方法将引发下一个异常;如何访问CountriesController的
创建
更新
方法中的
@country
实例变量?(@country只是一个例子;这必须适用于我的所有资源)

为什么不在controller中处理它?
解决方案很多,但我无法真正理解您的应用程序是如何工作的。

您可以将信息添加到控制器中的
参数
散列中,而不是将其设置为表单字段:

@item.update(item_params.merge(moduser: current_user.username, modtime: Time.now))

或者,您可以为具有这些属性的模型编写一个
关注点
。关注点应定义一个
更新后过滤器,该过滤器在每次更新时设置
moduser
modtime
。这是最干燥的解决方案。

这是一个很好的方法。我非常喜欢这个想法,但我还不知道如何做到这一点。我试图实现一些东西,但不完全正确。我已经用我的新代码更新了帖子。