#140 √ invalid
Jaroslaw Zabiello

Collecting data from many templates is broken

Reported by Jaroslaw Zabiello | February 19th, 2008 @ 07:11 PM | in 0.9

Implementation simple Rails' RJS with partial is real pain in ass in case of Merb. I know RJS may be evil for some maniacs, but it is also very usefull. In the following scenario I wanted to have the following Rails behaviour:

render :update do |page|
  page.replace_html :part1, :partial => 'part1'
  page.replace_html :part1, :partial => 'part1'
end

The problem is in collecting data from (sub)templates. Have a look:

app/views/customer/part1.html.erb:

PART1

app/views/customer/part2.html.erb:

PART2

app/views/customer/activities.html.erb:

<div id="part1"></div>
<div id="part2"></div>
<script type="text/javascript">
function refresh() {
  new Request({url: '/customer/activities'}).request();
}
window.setInterval('refresh()', 10000);
</script>

app/views/customer/activities.js.erb:

// mootools syntax, but it does not matter
$('mydiv1').set('html', '<%= escape_js @part1 %>'); 
$('mydiv2').set('html', '<%= escape_js @part2 %>');

app/controllers/custom.rb:

class Customer < Application
  provides :js, :html
  def activities
    if request.ajax?
      @part1 = render_html(:part1, :layout => false)
      @part2 = render_html(:part2, :layout => false)
    end
    render
  end

Merb cannot find the correct template files from inside AJAX loop. I tried to work around it in different ways. I used: render_html(:part1, :layout => false), render_html(:part1, :layout => false, :template=> 'customer/activities/part1') and render_html(:part1, :layout => false, :template => 'customer/acivities/part1.html.erb). Nothing works. The error message is quite stupid because it says that the template cannot be found even if provided filepath leads to correct file.

Comments and changes to this ticket

  • Justin Jones

    Justin Jones February 23rd, 2008 @ 04:03 AM

    It's looking for a .js.* extension, not a .html.*

  • Jaroslaw Zabiello

    Jaroslaw Zabiello February 26th, 2008 @ 12:25 PM

    That's right. render_html() should look for *.html.erb instead of *.js.erb. It looks to be too much dependent on the context of its calling.

    BTW, why Merb is using magic render_* methods instead of having only one render() method (with additional option for the type/mime it should return)? Bad, side effect of this approach is: the latest documentations (based on RDoc generation) does not reveal any render_* methoss because they are dynamic generated. Having meny render_* methods was existing in older Rails version. It was droped in favour of one and more flexible method.

  • Yehuda Katz (wycats)

    Yehuda Katz (wycats) March 1st, 2008 @ 06:15 AM

    • → Milestone changed from “” to “0.9”
    • → State changed from “new” to “open”

    Dup.

  • Yehuda Katz (wycats)

    Yehuda Katz (wycats) February 26th, 2008 @ 08:58 PM

    "More flexible methods" are dramatically slower. We use one render_* method per mime because it dramatically simplifies the render method (which previously had to do all sorts of hash investigation and hash passing in the render mixin).

    We should find a way to document the render_* methods, of course.

    I'll look into making sure it's possible to call .html from inside other mimes

  • Yehuda Katz (wycats)

    Yehuda Katz (wycats) March 4th, 2008 @ 11:10 PM

    • → Assigned user changed from “” to “Yehuda Katz (wycats)”
  • Yehuda Katz (wycats)

    Yehuda Katz (wycats) March 5th, 2008 @ 12:25 AM

    • → State changed from “open” to “invalid”

    Both render and partial take a :format option, which appears to work:

    class Foo < Application
      def ajax_response
        provides :js
        render
      end
    end
    

    ajax_response.js.erb

    <%= partial :foo, :format => :html %>
    

    _foo.html.erb

    Hello
    

    Result

    Hello
    

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »