Chris Pfohl
Chris Pfohl

Reputation: 19064

Rails Override Render

Is it possible to override Rails' render behavior for :html responses? I'd like to always render the same template (ignoring the magic view finding).

I'm writing a single page app, and this seems like it should be possible...basically if it's requested as :json it should render JSON, but if it's requested as :html it should pass the data on to the same view no matter what (where it will be rendered as JSON in the body).

Upvotes: 1

Views: 1686

Answers (3)

Anthony E
Anthony E

Reputation: 11235

Why not pass the JSON data as an instance variable?

controller

@json_data = whatever_model.to_json

application.html.erb

<script>
<%= @json_data %>
</script>

Upvotes: 0

Aldana
Aldana

Reputation: 383

What if you define one single view and then after every action on every controller you render that view? Like this:

app/controllers/home_controller.rb

class HomeController < ApplicationController
  def home
  end
end

app/views/home/home.html.erb

<!-- Whatever html code and script tags here -->

app/controllers/another_controller.rb

class AnotherController < ApplicationController
  def action
    render "home/home"
  end
end

You could even define an after_filter

Edit

I tried this and it works. The after filter doesn't seem to work though.

Upvotes: 0

lei liu
lei liu

Reputation: 2775

Try to delete the yield part on your application.html.erb, then you will alway get the application.html.erb without any partials.

Upvotes: 1

Related Questions