开发者

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 ?


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜