In Rails 3: Can a template image be associated with the view that's being displayed? -
sorry, i'm new @ rails i'll try specific can be.
in template have large "header" style image. swap image out image associated view being displayed. maybe can done using helper? don't know begin this.
i know make bunch of template pages , load each of them desired view, think thats lot of repeated lines of code load when want swap 1 image. have idea?
there few options depending on needs. first thing comes head create couple of helper methods. 1 call custom views , 1 call global layout.
for example, create file app/helpers/layout_helper.rb
module layouthelper def header_image_tag @header_image ||= 'whatever-my-default-image-is.png' image_tag @header_image end def header_image(image_path) @header_image = image_path end end
in layout file... e.g app/views/application.html.erb
. like:
<div id='banner'> <%= header_image_tag %> </div>
in individual view files don't want default image:
<% header_image 'other-image.png' %>
that should started. may want allow header_image_tag
take options pass onto image_tag, or set defaults can overridden.
the other thing can take advantage of content_for
, yield
blocks.
example... in custom views, put @ top of view:
<% content_for :banner %> <%= image_tag 'blah.png' %> <% end %>
and in layout
<div id='banner'> <%= yield :banner || image_tag 'my-default.png' %> </div>
Comments
Post a Comment