ruby-on-rails - 参数元编程

标签 ruby-on-rails parameters

如何以更简洁的方式更新这些非常相似的文本字段?下面的文本字段按给定名称命名 - 我还没有针对这个问题编辑它们。

  def update
    company = Company.find(current_user.client_id)
    company.text11 = params[:content][:text11][:value]
    company.text12 = params[:content][:text12][:value]
    company.text13 = params[:content][:text13][:value]
# etc
    company.save!
    render text: ""
  end

我尝试过使用 sendto_sym 但到目前为止没有运气......

最佳答案

[:text11, :text12, :text13].each do |s|
    company.send("#{s}=".to_sym, params[:content][s][:value])
end

如果都是增量数,则:

11.upto(13).map{|n| "text#{n}".to_sym}.each do |s|
    company.send("#{s}=".to_sym, params[:content][s][:value])
end

关于ruby-on-rails - 参数元编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11594496/

相关文章:

ruby-on-rails - apache 显示我的公共(public)目录的内容 - Rails + apache + Passenger + 本地 git + capistrano + ubuntu

html - 将旧内容(.html/.php 等)重新路由到 Ruby on Rails

java - JIT编译器优化: second method call with same parameter

javascript - 在Angular Controller 的回调中,为什么参数必须命名为 "$scope"?

ruby-on-rails - 显示记录计数 - Ruby on Rails - Ajax

ruby-on-rails - 设计和双重安全

java - 在Java中使用接口(interface)作为构造函数参数?

types - 我可以传入一个 "Type"作为函数参数吗?

c# - 包含函数和可选参数的字典

css - Rails 4 : how to identify and format links, 主题标签和模型属性中的提及?