Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #11614 (new defect)

Opened 3 weeks ago

Last modified 3 weeks ago

Cannot use flash object and other helpers in rjs helpers

Reported by: salkin Assigned to: core
Priority: normal Milestone: 2.x
Component: ActionPack Version: 2.0.1
Severity: normal Keywords:
Cc:

Description

When I create a Helper like

  def update_toolbox(opts = {})
    page[:toolbox_header].replace_html opts[:header] if opts[:header]
    page[:toolbox_content].replace_html opts[:content] if opts[:content]
    if footer = (opts[:footer] || flash[:notice])
      page[:toolbox_footer].replace_html footer
    end
  end

And call it from an .rjs template like so:

  page.update_toolbox(:content => 'foo')

I got the error

ActionView::TemplateError (ActionView::Helpers::JavaScriptProxy#to_str should return String) 
    /var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_view/helpers/prototype_helper.rb:968:in `join'
    /var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_view/helpers/prototype_helper.rb:968:in `arguments_for_call'
    /var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_view/helpers/prototype_helper.rb:1084:in `send'
    /var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_view/helpers/prototype_helper.rb:1084:in `call'
    /var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_view/helpers/prototype_helper.rb:1124:in `replace_html'
    app/helpers/renderings_helper.rb:7:in `update_toolbox'
    ...

It's the same if I call dom_id(some_record) from a helper. It works fine if I do everything in the RJS-Template itself, but that's not very DRY.

Change History

04/25/08 14:23:12 changed by salkin

I just discovered an old hack to accomplish that. Define in your ApplicationHelper

  def context
    page.instance_variable_get("@context").instance_variable_get("@template")
  end

Now you can access all the text/date/foohelpers with

   mydom = page.context.dom_id(record)
   page.context.flash[:notice]

to build a "brigde" between rjs-helpers (that use the page) and the basic helpers.

But I think this is a nasty hack and should be somehow fixed internally