如何以更简洁的方式更新这些非常相似的文本字段?下面的文本字段按给定名称命名 - 我还没有针对这个问题编辑它们。
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
我尝试过使用 send
和 to_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/