0

Let's say I've got AR class:

.........
  store :skin_properties, accessors: [
    :background_color,
    :font_size,
    :font_family,
    :color
  ]
.........

Which save values in this way:

{"background_color"=>"#f2f2f2", "font_size"=>"20px", "font_family"=>"Verdana", "color"=>"#000000"}

How can I insert it in edit.haml file as inline css?

I tried something like this, but it's not working ...

:sass
  body
    [email protected]_properties.each_with_index do |(k,v)|
      "#{k.gsub("_","-")}:#{v};"

5 Answers 5

1

You could try converting the hash to a style property of some tag.

Example: change

{"background_color"=>"#f2f2f2"}

to

style: {'background-color: #f2f2f2'}

Sign up to request clarification or add additional context in comments.

Comments

1

You are silently running ruby code(i.e. using -, instead of =).

Also, try using dasherize for such case:

:sass
    body
       = @user.skin_properties.map{|k,v| "#{k.dasherize}: #{v};" }.join

Comments

0

I believe you need to do it this way. I don't use haml so you'll need to convert it yourself:

:sass
  body
    <% @user.skin_properties.each_with_index do |(k,v)| %>
      <%= "#{k.gsub("_","-")}:#{v};" %>
    <% end %>

2 Comments

A bit nit picky, but what do you think about using sub over gsub for a little speed boost, since it looks like each property only contains at most 1 underscore.
Yes I think you're right, sub would be a bit more efficient since you are only substituting one character instead of changing all characters with the global gsub. Does that answer your question?
0
:sass
    body
       [email protected]_properties.map{|k,v| "#{k.gsub("_","-")}:#{v};" }.join

1 Comment

You should give some explanation for your answer.
0

Thanks everybody, came up with something like this:

- css = @user.skin_properties.map {|k,v| "#{k.dasherize}: #{v};" }.join("\n")
%style== body { #{css} }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.