Ruby on rails 设计配置

Ruby on rails 设计配置,ruby-on-rails,devise,Ruby On Rails,Devise,我目前想用Desive登录。除了电子邮件和密码之外,我想拥有一个用户名。我生成了迁移: class AddUsernameToBuyer < ActiveRecord::Migration def change add_column :buyers, :username, :string add_index :buyers, :username, unique: true end end class AddUsernameToBuyer

我目前想用Desive登录。除了电子邮件和密码之外,我想拥有一个用户名。我生成了迁移:

class AddUsernameToBuyer < ActiveRecord::Migration
  def change
    add_column :buyers, :username, :string
    add_index :buyers, :username, unique: true
  end
end
class AddUsernameToBuyer
我还在“注册”视图中添加了用户名:

<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 :username %> <br />
   <%= f.text_field :username %>
  </div>

  <div class="field">
    <%= f.label :email %><br />
    <%= f.email_field :email, autofocus: true %>
  </div>

  <div class="field">
    <%= f.label :password %>
    <% if @validatable %>
    <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="actions">
    <%= f.submit "Sign up" %>
  </div>
<% end %>

<%= render "devise/shared/links" %>
注册


(最少字符)

我将应用程序控制器用户名作为许可参数输入:

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username) }
    end

    def after_sign_up_path_for(resource)
      redirect_to root_path
    end
end
class ApplicationController
当我尝试注册时,我会被重定向到:
http://localhost:3000/buyers/sign_up

在我填写了详细信息后,我点击了注册,我希望自己被重定向到root_页面。然而,Desive将我重定向到
http://localhost:3000/buyers
它告诉我,即使我添加了空用户名,也不能有空用户名


如何更改/修复此行为

在为(资源)
登录路径之后,您需要在
中生成url。试试下面的代码

def after_sign_in_path_for(resource)
  root_path
end

若要重定向,您需要将
注册路径放在注册控制器之后。首先创建它以覆盖
设计
的本机控制器

class RegistrationsController < Devise::RegistrationsController
  protected

  def after_sign_up_path_for(resource)
    root_path
  end
end
类注册控制器
同样的问题,我被重定向到/buyers,我确实在“注册后”中更改了方法“不注册”,因为这是我的问题,但sameyes仍然很抱歉我这样做了,我在帖子中忘记了更改。我现在就去。错误仍然存在。