denniss
denniss

Reputation: 17589

rails content_for does not get executed when doing render?

So I have a content_for inside my view for logging in and in the sessions controller, I have render :new when the user does not enter valid credentials. However, when it does render :new, I get a black and white page without any css or js. This is how my content_for look like

<% content_for :head do %>
    <%= stylesheet_link_tag "user_sessions/new" %>
    <%= javascript_include_tag 'user_sessions/new.onready' %>
<% end %>

Is there a work around to make sure that the above code gets executed when I do render?

Upvotes: 6

Views: 3753

Answers (2)

RonaldOnRails
RonaldOnRails

Reputation: 41

<head><%= content_for :head %></head>

didn't work for me, but

<head><%= yield :head %></head>

worked, i guess this is the right to do

Just remembering: yield calls the content, and :head is the parameter for content_for that u put in your rendered view, so u can put pieces of code anywhere of the application.html u want

Upvotes: 4

Emily
Emily

Reputation: 18193

My guess would be that you're not including the content_for anywhere. In app/views/layouts/application.html.erb (or whichever layout you're using for this page) make sure you've got something like the following:

<head>
  <!-- your regular head content goes here -->
  <%= content_for :head %>
</head>

When you pass a block to content_for, the contents of the block are stored to be used elsewhere. The call to content_for without a block will then insert that stored content.

See the docs for content_for for more info.

Upvotes: 8

Related Questions