开发者

modify erb file on deployment

I have a fragment of JavaScript that I want to add to a page, but only in the production environment. Does rails have a way to insert or conditionally include on deployment. I know I could d开发者_JAVA技巧o "if Rails.env.production?" But I'd rather not do this condition check every time the page is loaded.


I wouldn't be worried about the overhead of one if statement.

Why not use a custom helper method:


def snippet
  if RAILS_ENV == "production"
   javascript_tag "whatever"
  elsif . . .
end
then you can use the same syntax:

<%= snippet %>

and you get a couple benefits:

  • access to other rails helpers
  • your config file won't be littered with raw html


What I do in this situation is create a constant in each environment's config file:

#config/environments/development.rb
SNIPPET = ""

#config/environments/production.rb
SNIPPET = "<script src='whatever.js'></script>"

#app/views/file.html.erb
<%= SNIPPET %>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜