Kyle C
Kyle C

Reputation: 4097

Using content_for inside a partial with Devise

I am in the process of adding Devise to my application. Everything seems to be working fine but I am struggling to put the sign in and sign up pages into a jquery container on my homepage. I tried pasting the forms in their directly but I was getting "undefined method" errors with the call to resource that devise makes with its forms.

  <div class="widget">
    <div id="tab-container">
      <ul>
  <li><a href="#create">Create Account</a></li>
  <li><a href="#signin">Sign In </a></li></ul>
<div id="create">

<%= yield :signup %>
    </div>
<div id="signin" style="height: 105px;">
  <ul>
  <%= yield :signin %></ul>
</div>
    </div>
  </div>

<% content_for :document_ready do %>   
  $('#tab-container').easytabs(); 
 <% end %>
 </script>

My Sign Up content

<h2>Sign up</h2>
  <% content_for :signup do %>

  <%= form_for(resource, :as => resource_name, :url => registration_path(resource_name))    
    do |f| %>
   <%= devise_error_messages! %>

  <div><%= f.label :email %><br />
  <%= f.email_field :email %></div>

  <div><%= f.label :username %><br />
    <%= f.text_field :username %></div>

    <div><%= f.label :password %><br />
    <%= f.password_field :password %></div>

  <div><%= f.label :password_confirmation %><br />
   <%= f.password_field :password_confirmation %></div>

  <div><%= f.submit "Sign up" %></div>
    <% end %>
   <% end %>

and my Sign in content

 <h2>Sign in</h2>
  <% content_for :signin do %>

   <%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do  
   |f| %>
 <div id="login_form">
 <div><%= f.label :email %><br />
  <%= f.email_field :email %></div>

 <div><%= f.label :password %><br />
 <%= f.password_field :password %></div>

<% if devise_mapping.rememberable? -%>
<div><%= f.check_box :remember_me %> <%= f.label :remember_me %></div>
 <% end -%>

  <div><%= f.submit "Sign in" %></div>
  </div>
<% end %>
<% end %>

Is it not a good idea to use content_for inside a partial? Is the yield not rendering before the tab container is called? Thanks in advance!

Upvotes: 0

Views: 535

Answers (1)

Kyle C
Kyle C

Reputation: 4097

Found the answer here Devise form within a different controller.

Add this to your application helper

 def resource_name
   :user
 end

 def resource
   @resource ||= User.new
 end

 def devise_mapping
   @devise_mapping ||= Devise.mappings[:user]
 end

Upvotes: 1

Related Questions