Rails Override Render - ruby-on-rails

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).

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

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.

Why not pass the JSON data as an instance variable?
controller
#json_data = whatever_model.to_json
application.html.erb
<script>
<%= #json_data %>
</script>

Related

Can I render rails welcome page from my controller?

When I want to show default welcome page by routes.rb,
I can add route like this:
root "rails/welcome#index"
Then, I want to render the same template in my controller out of curiosity.
The template is in rails/railties/lib/rails/templates/rails/welcome/index.html.erb
I thought `render "controller/action" works for my own files,
but it doesn't seem to work for built-in files.
def index
render "rails/welcome/index" # This shows error `Missing template rails/welcome/index`.
end
So, Is there a handy way to render built-in template?
Yes you can render the rails welcome page from your controller by using the Rails::WelcomeController
class WelcomeController < Rails::WelcomeController
end
Don't define the index method in WelcomeController. Rails will pick the index method of Rails::WelcomeController and will render the default welcome page.

Shared resource for partial in Rails

Premise: I'm quite new to Rails. I'm trying to render a partial in the application layout that will have to display some Event objects. The partial will have to be displayed in every page of the application (it's basically a sidebar). I am aware that I should pass a local variable to the partial, like
<%= render partial: "shared/aside", locals: {events: #events} %>
But this will only work if I define #events in every single controller of the application. Is there a way of setting it globally?
It might be worth noting that events might not be the only resource needed to the partial.
You could do a before action in the application controller. The before action would go at the top of the file, the private method at the bottom
class ApplicationController < ActiveController::Base
# Place at top of file
before_action :set_events
...your other code...
# Place at bottom of file
private
def set_events
#events = Event.all
end

Get html code of erb template in controller

I have home_controller and there are [:index, :process_img] actions within.
I need to get the whole html source of :index action from the action :process_img. Need to access that code in controller.
class HomeController < ActionController::Base
def index
end
def process_img
index_html_code = "the html source of index action should be here"
end
end
How can I achieve that? Thanks in advance!
You can use render_to_string (renders according to the same rules as render, but returns the result in a string instead of sending it as the response body to the browser):
render_to_string :index
Although I think render_to_string is an idiomatic option here's a way to do it which would work in plain ruby as well:
ERB.new(File.read "app/views/home/index.html.erb").result binding

rails: use jbuilder template in various controller methods

Is it possible to reuse jbuilder-template in another controller method? In other words: how to explicitly say controller method to use concrete jbuilder-template?
From Rails guide.
Rendering an Action's Template from Another Controller.
What if you want to render a template from an entirely different
controller from the one that contains the action code? You can also do
that with render, which accepts the full path (relative to app/views)
of the template to render. For example, if you're running code in an
AdminProductsController that lives in app/controllers/admin, you can
render the results of an action to a template in app/views/products
this way
def my_action
# some code here
render "products/show"
end
def my_action
# some code here
render "products/show.json"
end
or
def my_action
# some code here
render "show.json"
end
without the .json it will try to render an html file.

Is it possible to use partials with the layout option inside of a controller?

When try I following code in a controller, the view renders without using the layout
def xyz
render :partial => 'platinum_home', :layout => 'platinum_layout'
end
But If I do the following inside the partial
<% render(:layout => "platinum_layout") do %>
blah blah blah
<% end %>
It works just fine, is the first example not possible using rails?
In your controller at the top add the following:
class SomeController < ApplicationController
layout "platinum_layout", :only => :xyz
Partial rendering in a controller is most commonly used together with Ajax calls that only update one or a few elements on a page without reloading. Rendering of partials from the controller makes it possible to use the same partial template in both the full-page rendering (by calling it from within the template) and when sub-page updates happen (from the controller action responding to Ajax calls). By default, the current layout is not used.
So to use current layout for your just used.
def xyz
render :partial => 'platinum_home', :layout => true
end

Resources