开发者

How to add image/template/js/css load paths based on variable within app controller?

开发者 https://www.devze.com 2023-02-02 12:23 出处:网络
So, I have a bunch of partners whose templates and images are stored below the view, public/images, public/stylesheets, public/javascripts directories.

So, I have a bunch of partners whose templates and images are stored below the view, public/images, public/stylesheets, public/javascripts directories.

For example, images for a partner 'foo' are stored in public/images/foo

This partner is an instance variable @partner which is accessible at the application level.

Problem is, I'm doing this all over the place: <%= image_tag "/images/#{@partner}/image.jpg" %> or within locations_controller: <% redirect_to "/locations/#{@partner}/index" %> ...

One reason 开发者_Python百科for the load path part of the question rather than using helpers: we have to specifically <% render :template => "/locations/#{@partner}/index" %> since Rails looks in /locations/index by default.

How could I make this easier on myself? How could I add to load path once I have @partner?

Using Ruby 1.8.7 and Rails 2.3.4


Your redirection in the controller is really bad practice

you should do something like:

redirect_to get_path(@partner)

then in your controller

def get_path(partner)
  case partner
  when "partner1"
    partner1_path
  ...
  end
end

Concerning your pictures, you should create a helper.

def get_pic(partner, image)
  image_tag "/images/#{partner}/#{image}"
end

And in your view

<%= get_pic(@partner, "image.jpg" %>
0

精彩评论

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