Image_tag problem

Hiall,

I want to make an image_tag from within a controller in order to be
able to present a link (with a status image) in a view. Here is my
controller method (in file webca_controller.rb, hence WebcaController)

def untouched_status_image_tag
image_tag(“open”, { :alt => “Offen”, :title => “Offen”, :size =>
“12x12”, :class => “b_icon” } )
end

I have a myapp/public/images/open.png in place, however this seems not
enough :slight_smile: When executing the call to the above method, rails complains
with the following:

You have a nil object when you didn’t expect it!
The error occured while evaluating nil.request

…/actionpack-1.12.0/lib/action_view/helpers/asset_tag_helper.rb:150:in
compute_public_path' .../actionpack-1.12.0/lib/action_view/helpers/asset_tag_helper.rb:120:inimage_path’
…/actionpack-1.12.0/lib/action_view/helpers/asset_tag_helper.rb:135:in
image_tag' #{RAILS_ROOT}/app/controllers/webca_controller.rb:90:inuntouched_status_image_tag’

compute_public_path looks like so:

def compute_public_path(source, dir, ext)
source = “/#{dir}/#{source}” unless source.first == “/” ||
source.include?(":")
source = “#{source}.#{ext}” unless
source.split("/").last.include?(".")
source = “#{@controller.request.relative_url_root}#{source}” unless
%r{^[-a-z]+://} =~ source
source = ActionController::Base.asset_host + source unless
source.include?(":")
source
end

Why does @controller evaluate to nil in there? What is it that I’m
doing wrong again :slight_smile:

Thx for your patience with me :slight_smile:

cheers
Martin

How about:

tag(‘img’, options_hash)

You’ll have to break out the :height and :width options, but that’s
pretty
much the only difference.

See:
Peak Obsession

View this message in context:
http://www.nabble.com/image_tag+problem-t1666742.html#a4517614
Sent from the RubyOnRails Users forum at Nabble.com.

Thx for the tip! This got me a bit further, unfortunately only to
reveal that @controller is definitely nil in my setting. I have a
controller class implementing a method using helpers from various
ActionView::Helpers modules

class MyController

include ActionView::Helpers::TagHelper
include ActionView::Helpers::AssetTagHelper
include ActionView::Helpers::UrlHelper

def link_to_ca_module(ca_module_id)
assigned_ca_module = …
status_image_tag = …
action_name = …

link_to(status_image_tag + assigned_ca_module.ca_module.module_name,
    {   :controller => "my",
        :action => action_name,
        :id => @current_communal_audit_id
    },
    { :class => "a-done" })

end
end

So I’m basically calling Action::View::Helpers from my controller. Why
isn’t the @controller variable properly set in these Helpers? Am I not
supposed to use this methods from a controller? Should they only be
called from views? How do I create a list of links then in my
controller?

Too many questions :slight_smile: Any takers?

cheers
Martin

Hiall,

Yihaa I solved the problem! Never use ActionView::Helpers from a
controller it seems, instead use ActionController::Base.url_for in the
controller to setup (possibly a hash of) your data for a call to
link_to in a view.

Here is a snippet of my code that displays a link to a controller
action with a dynamically changed status image, based on the status of
the action (defined in a simple lookup table in my domain model)

The following code walks through all assigned audit_modules for a
particular audit, For each assigned audit_module it determines its
current status according to a simple lookup table and chooses an
appropriate image for that status. It then decides which
controller/action/id set to use to link to a page where one can edit
the details of this audit_module

MODEL CODE

class Audit < ActiveRecord::Base
has_many : assigned_audit_modules

end

class AuditModule < ActiveRecord::Base
has_many : assigned_audit_modules

end

class AuditModuleStatusCode < ActiveRecord::Base
has_many : assigned_audit_modules

end

class AssignedAuditModule < ActiveRecord::Base
belongs_to :audit
belongs_to :audit_module
belongs_to :audit_module_status_code

end

CONTROLLER CODE

class AuditController

def navbar
@audit = Audit.find(session[:audit_id])

if not audit.nil?
  @links  = []
  @audit.assigned_audit_modules.each { |assigned_audit_module|

    # These all contain strings
    link_name = determine_link_name(assigned_audit_module)
    status_image_tag =

determine_status_and_make_according_image_tag(assigned_audit_module)
controller_name = determine_controller_name
action_name = determine_action_name

    url = url_for(:controller => controller_name, :action =>

action_name, :id => @audit.id)

    @links << { :name => link_name, :image => status_image_tag,

:url => url }
}
end
end

VIEW CODE

    <% for link in @links %>
  • <%= link_to_unless_current(link[:image] + link[:name], link[:url], { :class => "a-done" }) %>
  • <% end %>

Some of you may or may not find this useful :=)

cheers
Martin