Gyntonic
Gyntonic

Reputation: 364

How could I make a Google Font selection in Rails?

I would like to create a form in rails with all the Google Fonts as options, something like this:

<input .../>
    <datalist>
        <% @font.each do |font| %>
            <option><%= font %></option>
        <% end %>
    </datalist>
</input>

So the user could choose an option to be loaded in the CSS.

Is this possible?

Upvotes: 0

Views: 255

Answers (1)

Akash Pinnaka
Akash Pinnaka

Reputation: 475

Saving fonts in database is not recommended.

Instead you could achieve this with javascript.

In your respective controller, give font hash.

In Controller

@fonts = {"arial" => "'Arial', sans-serif", "verdana" => "'Verdana', sans-serif"} 

In your View

<select id="select-font">
  <% @fonts.each do |key, value| %>
    <option value=<%= value %>><%= key.capitalize %></option>
  <% end %>
</select>

In javascript(jQuery)

$(document).ready(function() {
  $("#select-font").on("change", function() {
    $("body").css("font-family" : $("#select-font").value);
  });
});

Upvotes: 1

Related Questions