Ruby on rails RoR使用HAML将图像路径放入数据属性中

Ruby on rails RoR使用HAML将图像路径放入数据属性中,ruby-on-rails,haml,Ruby On Rails,Haml,我开始使用HAML,但遇到了一个问题 我需要将image\u path()url放入元素数据属性中 假设我的图像是app/assets/images/foo/bar/image.png 例如,我想出了如何获得图像\u路径 %h1 #{image_path ("foo/bar/image.png")} / will return /资产/foo/bar/image-944cf331269515d31d4ab23897df6c217e4593a942af756d4433a4987961cc29.p

我开始使用HAML,但遇到了一个问题

我需要将
image\u path()
url放入元素
数据属性中

假设我的图像是
app/assets/images/foo/bar/image.png

例如,我想出了如何获得
图像\u路径

%h1 #{image_path ("foo/bar/image.png")}
/ will return
/资产/foo/bar/image-944cf331269515d31d4ab23897df6c217e4593a942af756d4433a4987961cc29.png

这很好。现在我想把这个字符串放到元素的数据属性中,例如在
h1

这就是我尝试过的:

%h1{data: {'foo' => #{image_path ("foo/bar/image.png")} }}
/ returns an error
%h1{data: {'foo' => '#{image_path ("foo/bar/image.png")}' }}
/ returns
/ <h1 data-foo="#{image_path ('foo/bar/image.png')}"></h1> 
%h1{数据:{'foo'=>{image_路径(“foo/bar/image.png”)}
/返回一个错误
%h1{data:{'foo'=>'{image_路径(“foo/bar/image.png”)}}}
/返回
/  
这就是我要找的:

<h1 data-foo="/assets/foo/bar/image-944cf331269515d31d4ab23897df6c217e4593a942af756d4433a4987961cc29.png"></h1>

正确的方法是什么


谢谢

你差不多接近了,但也许你不知道“string”和“string”的区别

在Ruby中,带“string”的字符串不能替换任何内容。您需要使用“string”调用一个字符串,您可以使用inside#{}在该字符串中使用ruby代码

在控制台中测试以下内容

"#{1+1}" # outputs "2"
'#{1+1}' # outputs "#{1+1}"
现在回到您的问题:

%h1{data: {'foo' => '#{image_path ("foo/bar/image.png")}' }}
change to
%h1{data: {'foo' => "#{image_path ('foo/bar/image.png')}" }}
但是你不需要那些字符串操作,你可以直接使用

%h1{data: {'foo' => image_path ("foo/bar/image.png") }}
或者,如果您使用ruby 2.2.1,您可以更直接地使用它

%h1{data: {"foo": image_path ("foo/bar/image.png") }}
或者让它完全简短易读

%h1{"data-foo": image_path ("foo/bar/image.png")}
在Ruby 2.2.1中,哈希的行为发生了一些变化

2.1.2 :008 >   {"foo": "bar"}
SyntaxError: (irb):8: syntax error, unexpected ':', expecting => {"foo": "bar"}
   ^

2.2.1 :003 >   {"foo": "bar"}
 => {:foo=>"bar"}

这就是为什么我建议的最后两个选项只在2.2.1中起作用的原因。

你几乎接近了,但也许你不知道“string”和“string”的区别

在Ruby中,带“string”的字符串不能替换任何内容。您需要使用“string”调用一个字符串,您可以使用inside#{}在该字符串中使用ruby代码

在控制台中测试以下内容

"#{1+1}" # outputs "2"
'#{1+1}' # outputs "#{1+1}"
现在回到您的问题:

%h1{data: {'foo' => '#{image_path ("foo/bar/image.png")}' }}
change to
%h1{data: {'foo' => "#{image_path ('foo/bar/image.png')}" }}
但是你不需要那些字符串操作,你可以直接使用

%h1{data: {'foo' => image_path ("foo/bar/image.png") }}
或者,如果您使用ruby 2.2.1,您可以更直接地使用它

%h1{data: {"foo": image_path ("foo/bar/image.png") }}
或者让它完全简短易读

%h1{"data-foo": image_path ("foo/bar/image.png")}
在Ruby 2.2.1中,哈希的行为发生了一些变化

2.1.2 :008 >   {"foo": "bar"}
SyntaxError: (irb):8: syntax error, unexpected ':', expecting => {"foo": "bar"}
   ^

2.2.1 :003 >   {"foo": "bar"}
 => {:foo=>"bar"}

这就是为什么我建议的最后两个选项只在2.2.1中起作用的原因。

你几乎接近了,但也许你不知道“string”和“string”的区别

在Ruby中,带“string”的字符串不能替换任何内容。您需要使用“string”调用一个字符串,您可以使用inside#{}在该字符串中使用ruby代码

在控制台中测试以下内容

"#{1+1}" # outputs "2"
'#{1+1}' # outputs "#{1+1}"
现在回到您的问题:

%h1{data: {'foo' => '#{image_path ("foo/bar/image.png")}' }}
change to
%h1{data: {'foo' => "#{image_path ('foo/bar/image.png')}" }}
但是你不需要那些字符串操作,你可以直接使用

%h1{data: {'foo' => image_path ("foo/bar/image.png") }}
或者,如果您使用ruby 2.2.1,您可以更直接地使用它

%h1{data: {"foo": image_path ("foo/bar/image.png") }}
或者让它完全简短易读

%h1{"data-foo": image_path ("foo/bar/image.png")}
在Ruby 2.2.1中,哈希的行为发生了一些变化

2.1.2 :008 >   {"foo": "bar"}
SyntaxError: (irb):8: syntax error, unexpected ':', expecting => {"foo": "bar"}
   ^

2.2.1 :003 >   {"foo": "bar"}
 => {:foo=>"bar"}

这就是为什么我建议的最后两个选项只在2.2.1中起作用的原因。

你几乎接近了,但也许你不知道“string”和“string”的区别

在Ruby中,带“string”的字符串不能替换任何内容。您需要使用“string”调用一个字符串,您可以使用inside#{}在该字符串中使用ruby代码

在控制台中测试以下内容

"#{1+1}" # outputs "2"
'#{1+1}' # outputs "#{1+1}"
现在回到您的问题:

%h1{data: {'foo' => '#{image_path ("foo/bar/image.png")}' }}
change to
%h1{data: {'foo' => "#{image_path ('foo/bar/image.png')}" }}
但是你不需要那些字符串操作,你可以直接使用

%h1{data: {'foo' => image_path ("foo/bar/image.png") }}
或者,如果您使用ruby 2.2.1,您可以更直接地使用它

%h1{data: {"foo": image_path ("foo/bar/image.png") }}
或者让它完全简短易读

%h1{"data-foo": image_path ("foo/bar/image.png")}
在Ruby 2.2.1中,哈希的行为发生了一些变化

2.1.2 :008 >   {"foo": "bar"}
SyntaxError: (irb):8: syntax error, unexpected ':', expecting => {"foo": "bar"}
   ^

2.2.1 :003 >   {"foo": "bar"}
 => {:foo=>"bar"}
这就是我建议的最后两个选项仅在2.2.1中有效的原因