I am implementing a user registry with Devise 4.3.0 in Rails 5.1.3. Each user belongs to a Community and if the name of the community is provided in the registry a new community must be created next to the user.
The models:
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
belongs_to :community, optional: true
accepts_nested_attributes_for :community
end
class Community < ApplicationRecord
has_many :users
end
I have created a driver for user registration:
class User::RegistrationsController < ::Devise::RegistrationsController
before_action :configure_sign_up_params, only: [:create]
before_action :configure_community_update_params, only: [:update]
# GET /resource/sign_up
def new
super
resource.build_community
logger.info('-------NEW REGISTRATION')
logger.info(resource.community_id)
end
# POST /resource
def create
super
end
protected
# If you have extra params to permit, append them to the sanitizer.
def configure_sign_up_params
devise_parameter_sanitizer.permit(:sign_up, keys: [community_attributes: [:name]])
end
# If you have extra params to permit, append them to the sanitizer.
def configure_community_update_params
devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
end
end
In the custom view I include a form for the community:
<h2>Sign up</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<div class="field">
<%= f.label :password %>
<% if @minimum_password_length %>
<em>(<%= @minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<p> Comunidad </p>
<%= f.fields_for :community do |c| %>
<%= c.label :community_name %><br />
<%= c.text_field :name %>
<% end %>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
The problem I have is that the text box is not shown in the view and I have doubts about whether it would be correctly associated with the user even if it were displayed.
If you try to create a user, it is created correctly but without associating any Community. The log shows the following:
Started GET "/users/sign_up" for 127.0.0.1 at 2018-02-21 11:50:22 +0100
Processing by User::RegistrationsController#new as HTML
Rendering devise/registrations/new.html.erb within layouts/application
Rendered devise/shared/_links.html.erb (24.0ms)
Rendered devise/registrations/new.html.erb within layouts/application (88.0ms)
-------NEW REGISTRATION
Completed 200 OK in 1972ms (Views: 1760.1ms | ActiveRecord: 11.0ms)
Started POST "/users" for 127.0.0.1 at 2018-02-21 11:50:42 +0100
Processing by User::RegistrationsController#create as HTML
Parameters: {"utf8"=>"V", "authenticity_token"=>"C8c090OppHEve2JyNR+exuwmLNYtT
EaT11+6WUERwNs4DupDg+wWtLgdzmaPudgRCXEH/PpwHCzwSrk7g2RkhA==", "user"=>{"email"=>
"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
(77.0ms) begin transaction
User Exists (0.0ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = ? LIMIT ? [["email", "[email protected]"], ["LIMIT", 1]]
SQL (19.0ms) INSERT INTO "users" ("email", "encrypted_password", "created_at"
, "updated_at") VALUES (?, ?, ?, ?) [["email", "[email protected]"], ["encrypte
d_password", "$2a$11$OXep2cdfnXVJJOBT2CKKx.mBPULbonEO/qtCfaYHBd5v3zfjT55iq"], ["
created_at", "2018-02-21 10:50:44.205807"], ["updated_at", "2018-02-21 10:50:44.
205807"]]
(174.0ms) commit transaction
(0.0ms) begin transaction
SQL (23.0ms) UPDATE "users" SET "sign_in_count" = ?, "current_sign_in_at" = ?
, "last_sign_in_at" = ?, "current_sign_in_ip" = ?, "last_sign_in_ip" = ?, "updated_at" = ? WHERE "users"."id" = ? [["sign_in_count", 1], ["current_sign_in_at",
"2018-02-21 10:50:44.870845"], ["last_sign_in_at", "2018-02-21 10:50:44.870845"
], ["current_sign_in_ip", "127.0.0.1"], ["last_sign_in_ip", "127.0.0.1"], ["updated_at", "2018-02-21 10:50:44.916848"], ["id", 27]]
(122.0ms) commit transaction
Redirected to http://localhost:3000/
Completed 302 Found in 2864ms (ActiveRecord: 415.0ms)
Started GET "/" for 127.0.0.1 at 2018-02-21 11:50:45 +0100
Processing by WelcomeController#index as HTML
Rendering welcome/index.html.erb within layouts/application
Rendered welcome/index.html.erb within layouts/application (7.0ms)
User Load (11.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 27], ["LIMIT", 1]]
Completed 200 OK in 1264ms (Views: 1186.2ms | ActiveRecord: 11.0ms)
I have followed several guides and reviewed other similar problems but I can not move forward. Thanks.