guiding5
guiding5

Reputation: 139

Rails 3.1 Assets pipeline and JS preprocessing

I want to put some user preferences in my JavaScript. Previously I've done it by rendering my JavaSript file preferences.js.erb by controller via <%= javascript_link_tag ...>. How it should be cooked now with 3.1 Assets pipeline ?

Upvotes: 0

Views: 583

Answers (1)

Lev Lukomskyi
Lev Lukomskyi

Reputation: 6667

In no way.. application.js is static - on production it is generated into one big static file by rake assets:precompile command.

So you can include separate file preferences.js.erb as you proposed or render preferences directly on page - second is better because you save 1 request (but worse because they won`t be cached). To minimize size of preferences it would be better to present them in JSON, like this:

def user_preferences
  javascript_tag "
    var user_settings = #{ActiveSupport::JSON.encode(current_user.settings)}
  "
end

Upvotes: 1

Related Questions