如何替换haml中字符串的下划线
我使用rails_admin
我的部分之一是这样的:
%b= questionnaire.title
- CSV.parse(questionnaire.content, :headers => true, :col_sep => ",") do |row|
- row.to_hash.each do |key, value|
= succeed value do
%b= key + " : "
但键有时是这样的“I_dont_want_underscore”
我尝试了这个:
%b= questionnaire.title
- CSV.parse(questionnaire.content, :headers => true, :col_sep => ",") do |row|
- row.to_hash.each do |key, value|
= succeed value do
%b= key.gsub!-'_',' ') + " : "
但后来我得到了这个错误显示:无法转换冻结字符串(或类似的东西) 然后我尝试复制
%b= questionnaire.title
- CSV.parse(questionnaire.content, :headers => true, :col_sep => ",") do |row|
- row.to_hash.each do |key, value|
= succeed value do
%b= key.dup.gsub!-'_',' ') + " : "
但服务器不再响应...怎么回事? 最后 我尝试在 application_helper.rb 中添加 def
def sub_underscore
self.dup.gsub!-'_',' ')
end
,
%b= questionnaire.title
- CSV.parse(questionnaire.content, :headers => true, :col_sep => ",") do |row|
- row.to_hash.each do |key, value|
= succeed value do
%b= key.sub_underscore + " : "
但出现此错误:“此字符串没有方法 sub_underscore”
有什么想法吗?
I use rails_admin
One of my partial is like this :
%b= questionnaire.title
- CSV.parse(questionnaire.content, :headers => true, :col_sep => ",") do |row|
- row.to_hash.each do |key, value|
= succeed value do
%b= key + " : "
but key is sometimes like this "I_dont_want_underscore"
I tried this :
%b= questionnaire.title
- CSV.parse(questionnaire.content, :headers => true, :col_sep => ",") do |row|
- row.to_hash.each do |key, value|
= succeed value do
%b= key.gsub!-'_',' ') + " : "
but then I've got this error showing : Can't convert frozen string (or something like this)
Then I tried to duplicate
%b= questionnaire.title
- CSV.parse(questionnaire.content, :headers => true, :col_sep => ",") do |row|
- row.to_hash.each do |key, value|
= succeed value do
%b= key.dup.gsub!-'_',' ') + " : "
But then server does not respond anymore...how come ?
finally
I tried to put a def in my application_helper.rb
def sub_underscore
self.dup.gsub!-'_',' ')
end
and
%b= questionnaire.title
- CSV.parse(questionnaire.content, :headers => true, :col_sep => ",") do |row|
- row.to_hash.each do |key, value|
= succeed value do
%b= key.sub_underscore + " : "
But I get this error : "no method sub_underscore for this string"
Any ideas ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
gsub!
您可以就地修改字符串。这不是你需要的。尝试使用gsub
代替。With
gsub!
you are modifying the string in place. That's not what you need here. Try usinggsub
instead.