rails/rails — pull request #15224 by chingor13 (2014-05-21 16:53:00+00:00)
Title: ActionView::LookupContext js/html fallback (#10525 redo)

I made pull request #10525 about a year ago and received comments recently. I messed up that merge from rails so that pull request somehow has an extra 100 commits or so.

`ActionView::LookupContext` is hardcoded to append the :html format whenever :js is set as the format. A user may want to extend this behavior to their own custom defined formats (for example a :mobile format could fallback to :html as well).

This is what it was for:

----

This current behavior is the magic behind allowing you to render an html partial within a javascript response.

```ruby
# in new.js.erb
# will render the _form.html.erb partial
render partial: 'form', locals: {object: @object}
```

This change allows you to specify your own fallbacks easily. In somewhere like `config/initializers/mime_types.rb`:
```ruby
MimeTypes.register_alias "text/html", :mobile
ActionView::LookupContext.format_fallbacks[:mobile] = [:html]
```
Now, if you have a request where the format is :mobile, it will try to render a mobile template first, and fallback to the html template. Additionally, you could render an html partial within a mobile template.

This behavior is already being tested in `actionpack/test/template/lookup_context_test.rb`.