Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.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 Ruby-key&;价值_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails Ruby-key&;价值

Ruby on rails Ruby-key&;价值,ruby-on-rails,ruby,Ruby On Rails,Ruby,我使用ruby键和值字段将jquery函数保存到数据库中,一切正常,但如何分离值参数 def update_background key = "#{current_user}.profile.background" settings = Settings.find_by_key(key) || Settings.new(key: key) settings.value = params[:bg_id], params[:color_id] respond_to do |form

我使用ruby键和值字段将jquery函数保存到数据库中,一切正常,但如何分离值参数

def update_background
  key = "#{current_user}.profile.background"
  settings = Settings.find_by_key(key) || Settings.new(key: key)
  settings.value = params[:bg_id], params[:color_id]

  respond_to do |format|
    format.json do
      if settings.save
        render text: "success"
      else
        render text: "failure"
      end
    end
  end
end
上面的代码工作正常,但问题是我的css格式不正确。代码通常是这样显示的

background:  url(../assets/stripes.png rgb(76, 72, 128))
在我的助手文件中

def saved_background
  key = "#{current_user}.profile.background"
  settings = Settings.find_by_key(key)
   if settings
     "url(#{settings.value})"
  end
end
如何在值中分隔这两个参数,使代码看起来像

background: rgb(76, 72, 128) url(../assets/stripes.png)

您基本上是将数组保存到
settings.value
,并在
url
字符串中使用它

将设置分离为单独的值,或单独使用每个元素:

"url(#{settings.value[0]}) #{settings.value[1]}"

编辑啊,这是一个AR字段——所以它很可能被保存为数组的字符串代表。检查起来很琐碎,你应该已经检查过了

您需要以实际需要的格式保存它--因此:

以及:


使用上述方法时不输出值,但在删除数组索引号时,我可以看到值。有其他建议吗?@coletrain更新;错过了这是一个AR场。
settings.value = "url(#{params[:bg_id]}) #{params[:color_id]}"
def saved_background
  key = "#{current_user}.profile.background"
  settings = Settings.find_by_key(key)
  settings.value if settings
end