Reputation: 8030
I'm trying to build a nested form using Devise, keeping the devise featuring as sending an email when a new user signs up. I would like to have something like this:
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<%= f.fields_for(:information) do |info| %>
<div><%= info.text_field :name, :placeholder => 'Nome' %></div>
<div><%= info.text_field :surname, :placeholder => 'Cognome' %></div>
<% end %>
<div><%= f.email_field :email, :autofocus => true, :placeholder => 'E-mail' %></div>
<div><%= f.password_field :password, :placeholder => 'Password' %></div>
<div><%= f.password_field :password_confirmation, :placeholder => 'Conferma password' %></div>
<div><%= f.submit "Registrati", class: "btn btn-large btn-info" %></div>
<% end %>
In my route.rb
devise_for :users, :controllers => { :registrations => "users" }
In my user.rb
class User < ActiveRecord::Base
has_one :information, dependent: :destroy
# Include default devise modules. Others available are:
# :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable,
:validatable, :confirmable, :lockable
accepts_nested_attributes_for :information, update_only: true
end
and in information.rb
class Information < ActiveRecord::Base
belongs_to :user
end
in users_controller.rb
class UsersController < Devise::RegistrationsController
def new
super
resource.build_information
end
end
But nothing happen, or better the 2 fields name
and surname
don't appear, but I don't receive an error message
Upvotes: 0
Views: 79
Reputation: 3308
Here is what I would do,
I would move this line from the controller to the view(fields_for takes 2 parameters). Like below,
Controller
def new
super
resource.build_information #remove from here
end
View
<!--Added here -->
<%= f.fields_for(:information,resource.build_information) do |info| %>
Must work now!
OR
Just change
<%= form_for(resource, :as => res.....
to
<%= form_for(@resource, :as => res
Note "@", this will work. Remove build_information both from view and controller.
Upvotes: 1