Using content_for inside a controller

rails content_for
rails yield
rails content for title
rails controller layout

This is a weird requirement that may need another approach, but my brain is stuck.

I want to accomplish something like this:

class UsersController < ApplicationController
  before_filter Proc.new { share_params :user_name }, :only => :show
  render_djs
end

class ApplicationController < ActionController::Base
  include ActionView::Helpers::CaptureHelper
  def share_params(*shared)
    content_for(:djs) { shared.inspect }
  end

  def self.render_djs
    before_filter Proc.new {
      render :inline => "<%= yield(:djs) %>" if request.format.djs?
    }
  end
end

I want to use content_for because I may want to add content to the :djs yield in other filters.

However, this code raises undefined method output_buffer=.

I suppose I could use an instance variable, but this seems cleaner, doesn't it?


You need to use the #view_context method to reach the view context and then you can do the same as you would do in a view:

view_context.content_for(:something, view_context.render(partial: 'some_partial'))

content_for in controller as described in http://blog.yakitara.com , class ApplicationController < ActionController::Base # FORCE to implement content_for in controller. def view_context. super.tap do |view|. (@_content_for  it mentions Url.Content inside controller which I ended up using at the time – Omu Apr 20 '16 at 5:31. add a comment | 4 Answers Active Oldest Votes. 1. To get the


I found this helpful very much in case of setting title for my page.

I can just set content_for from controller

class PostsController < ApplicationController
  def index
    content_for :title, "List of posts"
    ...
  end
end

https://gist.github.com/hiroshi/985457 https://github.com/clmntlxndr/content_for_in_controllers

Layouts and Rendering in Rails, Rails uses content_for as its primary way to store content in one place for use in other views, …using an instance variable in the controller… Using the Add Controller Menu Option. The easiest way to create a new controller is to right-click the Controllers folder in the Visual Studio Solution Explorer window and select the Add, Controller menu option (see Figure 1). Selecting this menu option opens the Add Controller dialog (see Figure 2).


If like me you are just looking to set content in the controller like a title, then it's probably better to just use a variable that's automatically passed to views and helpers. eg.

controller:

class AController < ApplicationController
  before_action :set_title
  private
    def set_title
      @title = 'Email Subscription'
    end
end

and the helper:

module ApplicationHelper
  def title_suffix
    " - #{@title}" unless @title.nil?
  end
end

and the template:

<!DOCTYPE html>
<html>
<head>
  <title>Standard Title<%= title_suffix %></title>
...

Set your page title in the view template using content_for, Instead of… …using an instance variable in the controller… things_controller.rb. def show @page_title = 'Page Title' You can use the movie database context to query, edit, and delete movies. private MovieDBContext db = new MovieDBContext(); Strongly Typed Models and the @model Keyword. Earlier in this tutorial, you saw how a controller can pass data or objects to a view template using the ViewBag object.


you can't use content_for in controllers, even you knew view_context can provide the method. see the issue here: https://github.com/rails/rails/issues/4906

the method view_context always returns new object. when you call view_context.content_for(:somethin, 'content') it will store content in instance variable of the new object.

you can do an experiment in controllers like the following:

view_context.content_for(:title, 'hello')
view_context.view_flow.content # => {}

a = view_context
a.content_for(:title, 'hello')
a.view_flow.content # =>  {:title=>"hello"}

anyway, if you still want to use content_for in controllers, you can override view_contxt in controllers as a workaround. but I don't know whether any side effect.

def view_context
  @_view_context ||= super
end

Set your page title in the view template using content_for, Recently we had a feature request in an app: 'show the login box on every was happening in UsersController#new and UsersController#create): Now what happens when using #content_for is that each #content_for for a  Using our example script, you can pass data, variables, or parameters to the external URL and get dynamic content via jQuery Ajax. Bootstrap and jQuery Library. Before using the Bootstrap to create a modal popup, include the Bootstrap and jQuery library first.


I've found a cleaner way than setting content_for from the controller.

In my case I have sidebar which needs to be displayed for every view.

In my layout I have

<%= yield :sidebar %>

Then I have a partial called _set_sidebar.html.erb which does this

<% content for :sidebar do %>
   <% render :partial => "layouts/sidebar", locals => {:locs => locs} %>
<% end %>

Then I just put a one liner in every view that I want to also have that sidebar

<%= render :partial => "layouts/set_sidebar", locals => {:locs => locs}

Otherwise I was previously using this

https://gist.github.com/985457 with .html_safe when I used render_to_string

no content for you!, Controller is responsible for orchestrating the whole process of handling a In the following example for yield and content_for, we'll use the layout below  Start by using your can of compressed air to blow away any surface dust, pet hair, or human hair from the face of your controller. If you have an air compressor, you can also use that to clean


Rails : Layouts and Rendering 1 (yield, content_for, content_for , Calling content_for stores a block of markup in an identifier for later use. In order to access this stored content in other templates, helper modules or the layout,  To use route caching, you must convert any Closure routes to controller classes. If your application is exclusively using controller based routes, you should take advantage of Laravel's route cache. Using the route cache will drastically decrease the amount of time it takes to register all of your application's routes.


content_for (ActionView::Helpers::CaptureHelper), With the above resources : articles in our routes file, if we have a controller file Using Controllers, Views, and Layouts 8 yield and content_for in Using  Wipe down the controller with the mixture using a microfiber towel. Spray 2 to 3 spurts of your disinfectant onto a clean microfiber towel. Wipe down the entire surface of the controller. Once the clean side of the towel gets dirty, flip it to the other side. You can also use a soft cloth or lint-free paper towel.


Ruby on Rails Explained for Front-End Developers, i've got a helper method that calls content_for to get all of the content for a it'd be great if i could add some before filter in the controller and Use a small tool to push the button inside the tiny hole. Hold the button down for roughly 3-5 seconds. Connect the DS4 controller to the PS4 using a USB cable and press the PS button. If the light bar turns blue, the DS4 has paired.