Create users with devise from another Rails 5 controller

0

I am using devise for user authentication and I am trying to create view admin that can create users from that view.

Things to know:

I am using the ApplicationHelper to be able to login from the home instead of using the default login.

The idea is that the admin can create the users with other roles (ready, configured and running with CANCAN) that is why this view will include in the form the role field.

ApplicationHelper:

module ApplicationHelper

  def resource_name
   :user
  end
  def resource_name_request
   :request
  end

 def resource
   @resource ||= User.new
 end

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

end

admin_controller: If I do it in a static way, that insert works but it does not take the form variables

class AdminController < ApplicationController
      before_action :authenticate_user!
  def index
#    @user = User.new(:email => '[email protected]',
#                     :password => 'password',
#                     :password_confirmation => 'password') funciona pero no toma las variables del form

    @user = User.new(admin_params)
    @user.save
  end

  private

  def admin_params
  #  devise_parameter_sanitizer.permit(:sign_up, keys: [:name, :email, :password, :password_confirmation, :role])
    params.require(:admin).permit(:name, :email, :password, :password_confirmation, :role)
  end
end

index.html.erb: View of my controller Admin

<div id="conteiner_admin">
      <div class="container">
        <div align="center">
          <%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
          <%= devise_error_messages! %>

          <div class="field">
            <%= f.label :name %><br />
            <%= f.text_field :name %>
          </div>

          <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>

            <div class="field">

              <%= f.select(:role) do %>
                <% [['Admin', "admin"], ['User', "user"]].each do |c| -%>
                  <%= content_tag(:option, c.first, value: c.last) %>
                <% end %>
              <% end %>
            </div>

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

      </div>
</div>

My main problem is that I do not understand why strong parameters do not capture my form giving an error:

ActionController :: ParameterMissing in AdminController # index

I would like to know if it is something bad in the controller or is my problem with the strong parameters

    
asked by J.leo 26.08.2017 в 23:21
source

1 answer

0

in your strong paremeters, you are indicating that the key is admin , when in the form view the key corresponds to user , which you have initialized it in your AplicationHelper , such as resource_name

try to change

params.require (: admin) .permit (: name,: email,: password,: password_confirmation,: role)

for

params.require (: user) .permit (: name,: email,: password,: password_confirmation,: role)

It is worth mentioning that you must verify your routes, to see if the index method corresponds to the post verb of your form with the command

rake routes

    
answered by 20.05.2018 в 05:25