我使用Devise验证用户身份,然后为first_name,last_name和username添加了迁移。更改了简单的表单代码后,我的sign_up路径将我重定向到消息“您需要登录或注册才能继续。”没有任何错误名称。
我的网站链接也很奇怪,因为它具有sign_up.userhttp://localhost:3000/users/sign_up.user
这是我的代码,将我发送到设计/共享/链接的错误路径
<%- if devise_mapping.registerable? && controller_name != 'registrations' %>
<%= link_to "Sign up", new_user_registration_path(resource_name) %><br />
<% end %>
这是我在devise / registrations / new上的简单表单(尚未实现first_name,last_name和用户名。
<h2>Sign up</h2>
<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :email,
required: true,
autofocus: true,
input_html: { autocomplete: "email" }%>
<%= f.input :password,
required: true,
hint: ("#{@minimum_password_length} characters minimum" if @minimum_password_length),
input_html: { autocomplete: "new-password" } %>
<%= f.input :password_confirmation,
required: true,
input_html: { autocomplete: "new-password" } %>
</div>
<div class="form-actions">
<%= f.button :submit, "Sign up" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
还有我的路线...
Rails.application.routes.draw do
root to: 'pages#home'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
resources :items
get 'menu', to: 'items#menu'
get 'items_stock', to: 'items#stock'
devise_for :users, :controllers => { registrations: :registrations }
get 'users/:slug', to: 'users#show', as: :user_show
get 'my_profile', to: 'users#my_profile'
end
我也试图创建另一个控制器来重写,但是没有用...
class RegistrationsController < ApplicationController
def new
@user = User.new
end
def create
@user = User.new(user_params)
@user.save
if @user.save
redirect_to user_path(@user)
authorize @user
else
render :new
end
end
private
def user_params
params.require(:user).permit(:first_name, :last_name, :username, :email, :password, :password_confirmation)
end
end
我的路线
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
user_password PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
POST /users/password(.:format) devise/passwords#create
cancel_user_registration GET /users/cancel(.:format) registrations#cancel
new_user_registration GET /users/sign_up(.:format) registrations#new
edit_user_registration GET /users/edit(.:format) registrations#edit
user_registration PATCH /users(.:format) registrations#update
PUT /users(.:format) registrations#update
DELETE /users(.:format) registrations#destroy
POST /users(.:format) registrations#create
user_show GET /users/:slug(.:format) users#show
my_profile GET /my_profile(.:format) users#my_profile
我真的不知道wich文件可能会影响此。
这有点困难,因为在Devise之外这里有一些自定义文件,我看不到整个实现。我认为在您添加这些新字段之后发生这种情况是关键。如果您用此代码更新应用程序控制器,我敢打赌它将起作用,我认为您不允许正确使用新参数。
application_controller.rb
...
before_action :configure_permitted_parameters, if: :devise_controller?
...
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name, :last_name, : username])
devise_parameter_sanitizer.permit(:account_update, keys: [:first_name, :last_name, : username])
end