how to substitute underscore of a string in haml
-
27-10-2019 - |
Question
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 ?
Solution
With gsub!
you are modifying the string in place. That's not what you need here. Try using gsub
instead.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow