I am working on a rails app. I use devise for auth. I added name,surname,phone,schoolNumer variables to User. When press sign up button nothing happens just refreshing page. But login button is working. What is the problem?
registrations/new.html.erb
<div >
<form method="post">
<div ><%= image_tag('paulogo.png') %></div>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= render "devise/shared/error_messages", resource: resource %>
<div >
<%= f.text_field :name, autofocus: true, autocomplete: "name", placeholder:"Name", class:"form-control" %>
</div>
<div >
<%= f.text_field :surname, autofocus: true, autocomplete: "surname", placeholder:"Surname", class:"form-control" %>
</div>
<div >
<%= f.number_field :phone, autofocus: true, autocomplete: "phone", placeholder:"Phone Number", class:"form-control" %>
</div>
<div >
<%= f.number_field :schoolNumber, autofocus: true, autocomplete: "school number", placeholder:"School Number", class:"form-control"%>
</div>
<div >
<%= f.email_field :email, autofocus: true, autocomplete: "email", placeholder:"Email", class:"form-control" %>
</div>
<div >
<% if @minimum_password_length %>
<%= f.password_field :password, autocomplete: "off", placeholder:"Password (Min ".concat(@minimum_password_length.to_s) " Characters)", class:"form-control"%>
<% end %>
</div>
<div >
<%= f.password_field :password_confirmation, autocomplete: "off", placeholder:"Password Confirmation", class:"form-control" %>
</div>
<div >
<%= f.submit "Sign up", class:"btn btn-primary btn-block"%>
</div><div >
<%= render "devise/shared/links" %>
</div>
<% end %>
</form>
</div>
application_controller.rb
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
devise_parameter_sanitizer.permit(:account_update, keys: [:name])
devise_parameter_sanitizer.permit(:sign_up, keys: [:surname])
devise_parameter_sanitizer.permit(:account_update, keys: [:surname])
devise_parameter_sanitizer.permit(:sign_up, keys: [:phone])
devise_parameter_sanitizer.permit(:account_update, keys: [:phone])
devise_parameter_sanitizer.permit(:sign_up, keys: [:schoolNumber])
devise_parameter_sanitizer.permit(:account_update, keys: [:schoolNumber])
end
end
User.rb
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
end
And db/migrate/AddDetailsToUser.rb
class AddDetailsToUsers < ActiveRecord::Migration[7.0]
def change
add_column :users, :name, :string
add_column :users, :surname, :string
add_column :users, :phone, :integer
add_column :users, :schoolNumber, :integer
end
end
CodePudding user response:
If you check the server logs, you might see devise complaining about unpermitted params. You'll need to permit all the required keys at once.
devise_parameter_sanitizer.permit(:sign_up, keys: [:name, :phone, :schoolNumber, :surname, :email, :password, :password_confirmation])
CodePudding user response:
I solved the problem
routes.rb
Rails.application.routes.draw do
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
devise_for :users
devise_scope :user do
get '/users/sign_out' => 'devise/sessions#destroy'
post '/users/sign_up' => 'devise/registrations#create'
end
# Defines the root path route ("/")
# root "articles#index"
resources :users, only: [:show]
root to: "pages#home"
get '/about', to: 'pages#about'
get '/contact', to: 'pages#contact'
end
and I created the file views/user/show.html.erb