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

Ticket #8076 (closed defect: fixed)

Opened 2 years ago

Last modified 2 years ago

[PATCH] .html.erb not working for partials?

Reported by: sol Assigned to: core
Priority: normal Milestone: 1.x
Component: ActionPack Version: edge
Severity: normal Keywords:
Cc: simon@mintsource.org, tom, anthonyw, kamal, lifofifo

Description

The new naming scheme for template (eg. .html.erb) does not seem to work for partials. When using :partial => 'form' a filename "_form.html.erb" is not recognized. Will this be added?

Attachments

render_partial_with_html_template_format.diff (1.8 kB) - added by norbert on 05/24/07 10:22:39.
render_partial_with_format_extension_fix.diff (2.3 kB) - added by norbert on 06/27/07 10:10:15.
render_partial_with_format_extension_fix.2.diff (2.3 kB) - added by norbert on 06/29/07 23:24:10.

Change History

04/20/07 14:33:38 changed by technoweenie

It's added, but I'm probably going to remove it. It makes it impossible to reuse the partial in your RJS actions too.

04/29/07 10:59:05 changed by Si

  • cc set to simon@mintsource.org.

Can I get some clarification on this? Currently .html.erb partials are found from an erb procesing context, but not from an RJS action. Looks like page.insert_html(etc) looks for .js.html partials, as it's running from an rjs context, so renaming to straight .erb works as a fallback.

Those methods are specifically for dealing with html though, so can't the render reflect this? What's the aim regarding the removal mentioned, and where will this leave the naming strategy?

04/29/07 11:05:57 changed by Si

Oops, I meant page.insert_html looks for .js.erb

04/29/07 15:35:12 changed by technoweenie

  • status changed from new to closed.
  • resolution set to fixed.

You can hard code a partial name with render though:

render :partial => 'foo.html.erb'

However, someone else had a good use case for keeping the format in partials, so it looks like it's staying for now.

04/29/07 17:11:38 changed by Si

  • status changed from closed to reopened.
  • resolution deleted.

Thanks for update.

Regarding hard coding, I'd tried that, but ran into invalid variable name problems when partial names including format and builder reach partials.add_object_to_local_assigns!

controller.instance_variable_get("@#{partial_name}")

05/12/07 18:44:20 changed by tom

  • cc changed from simon@mintsource.org to simon@mintsource.org, tom.

05/19/07 19:19:13 changed by anthonyw

  • cc changed from simon@mintsource.org, tom to simon@mintsource.org, tom, anthonyw.

05/24/07 10:22:11 changed by norbert

Attached is a patch that makes :html the default template_format for all render :partial calls. The code is not pretty and it breaks some behaviour (hence the changed test) but it does solve this problem.

05/24/07 10:22:39 changed by norbert

  • attachment render_partial_with_html_template_format.diff added.

05/24/07 16:26:54 changed by josh

  • summary changed from .html.erb not working for partials? to [PATCH] .html.erb not working for partials?.

05/24/07 16:29:36 changed by technoweenie

As far as I know, you can have partials in other template renderers, such as RXML, so this may not always be appropriate.

Here's the main issue as I understand it. There are 2 ways that ActionView will set your template format. Either from params[:format], or from the respond_to block.

respond_to do |format|
  format.html # show.html.erb
  format.js # show.js.rjs
end

Now, the problem comes up when you try to use an html erb template in your ajax action. It's looking for *.js.erb now. For now, the solution is to just name it *.erb and leave the format out altogether. But, a lot of folks I've talked to ran into this problem. Obviously it's violating some least surprise principle.

If you come up with a good solution so it'll work as expected, please do send in a patch. If you come across some edge case that's breaking, a patch, or at least a failing test case would be great. Thanks.

05/31/07 12:18:32 changed by kamal

  • cc changed from simon@mintsource.org, tom, anthonyw to simon@mintsource.org, tom, anthonyw, kamal.

Worth nothing that DHH's RailsConf 2007 keynote slides also makes the assumption that partial in a format.js context should render the .html.erb template. See page 13.

Snippet:

format.js
  render(:update) do |page|
    # "people/_person.html.erb"
    page[:people].insert_html :top, :partial => person
    
    # $('person_1')
    page[person].visual_effect(:highlight)
  end
end

05/31/07 13:43:37 changed by Si

Nabbing that example, and in response to the "least surprise" principle mentioned by technoweenie, I think the method name says it all. It's not insert_erb or insert_js after all - I'm definitely expecting to insert html.

# "people/_person.html.erb"
page[:people].insert_html :top, :partial => person

05/31/07 13:52:55 changed by norbert

Before I wrote the current patch I made one that changes the render call in JavaScriptGenerator, but the problem is wider than that. I would rather fix it properly, but I'll upload the patch in a bit, for reference.

Also, comment 5 (the instance variable thing) is still an issue, I've got a patch for that too.

06/27/07 10:10:15 changed by norbert

  • attachment render_partial_with_format_extension_fix.diff added.

06/27/07 10:28:34 changed by norbert

Okay, finally. The patch I just sent allows you to use dots in partial names, which fixes this:

render :partial => 'foo.html.erb' 

but still not this:

render :partial => 'foo'


Even after trying several options, I'm not quite sure how to fix the latter. If someone has a good idea I would be willing to implement it, so Please Do Comment.

06/27/07 12:30:46 changed by lifofifo

  • cc changed from simon@mintsource.org, tom, anthonyw, kamal to simon@mintsource.org, tom, anthonyw, kamal, lifofifo.

06/29/07 23:24:10 changed by norbert

  • attachment render_partial_with_format_extension_fix.2.diff added.

07/01/07 00:04:06 changed by rick

  • status changed from reopened to closed.
  • resolution set to fixed.

(In [7158]) Allow you to render views with periods in the name. Closes #8076 [norbert]