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
<ul>
<% for link in @links %>
<li> <%= link_to_unless_current(link[:image] + link[:name],
link[:url], { :class => "a-done" }) %></li>
<% end %>
</ul>
Some of you may or may not find this useful :=)
cheers
Martin
On 5/23/06, Martin Gamsjaeger <gamsnjaga@gmail.com>
wrote:> 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 :-) Any takers?
>
> cheers
> Martin
>
> On 5/23/06, s.ross <cwdinfo@gmail.com> wrote:
> >
> > 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:
> >
http://api.rubyonrails.com/classes/ActionView/Helpers/TagHelper.html#M000488
> > --
> > View this message in context:
http://www.nabble.com/image_tag+problem-t1666742.html#a4517614
> > Sent from the RubyOnRails Users forum at Nabble.com.
> >
> > _______________________________________________
> > Rails mailing list
> > Rails@lists.rubyonrails.org
> > http://lists.rubyonrails.org/mailman/listinfo/rails
> >
>