数据库驱动的单选按钮ruby在轨道上

时间:2015-04-30 14:58:56

标签: ruby-on-rails ruby

我正在尝试使用单选按钮的值打开和关闭来更新我的条件表

这是视图

= form_tag('/admin/save',:action => 'update') do    
 = radio_button_tag("#{cols}_#{id}",1,checked = eval(check), options = {}) 
 = radio_button_tag("#{cols}_#{id}",0,checked = eval(negcheck), options = {})
 = submit_tag

这是控制器

def updateCondition
    params.each do |keys , value|
      key ="#{keys}"
      condition = key.split("_")[0]
      hospitalid =key.split("_")[1]
      if condition == "utf8" || condition == "authenticity" || condition == "commit"
        next        
      end
      Condition.find(hospitalid).update(:"#{condition}" =>  params["#{condition}_#{hospitalid}"])
    end
    render nothing: true
  end

参数是:

{"utf8"=>"✓",
 "authenticity_token"=>"",
 "abc_10000"=>"1",
 "commit"=>"Save changes",
 "def_10000"=>"1",


 }

这是我的问题:

1)为什么params没有显示我的所有单选按钮值? 2)处理utf8,authentication_token,除if语句以外的提交的任何方法吗?

1 个答案:

答案 0 :(得分:0)

这可能就是为什么params没有显示:

这对我来说很奇怪:checked = eval(check)应该checked: eval(check)checked == eval(check)

你为什么要设置options = {}?我认为选项应该作为哈希传递或者根本不传入。

关于#2,

if condition == "utf8" || condition == "authenticity" || condition == "commit"
  next        
end
Condition.find(hospitalid).update(:"#{condition}" =>  params["#{condition}_#{hospitalid}"])

您可以将其提取到单独的函数中:

def condition_valid?
  condition == "utf8" || condition == "authenticity" || condition == "commit"
end