在我的rails项目上使用Devise新注册时出错

时间:2015-10-31 22:00:36

标签: ruby-on-rails ruby devise

我正在使用Devise gem构建我的第一个rails应用程序进行身份验证。

登录效果很好,但新注册(注册)页面在PAGE上发送错误,不会让我注册。

见图片 enter image description here

这是强参数还是我的应用程序控制器的问题。非常感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

这可能是因为您在User模型中启用了validations,您指定的emailpassword不能为空。

因此,在尝试保存信息时,您必须提供email的{​​{1}}和password

更新

如果情况并非如此,则可能与强参数有关。请参阅this thread

user文件中,添加以下内容:

application_controller.rb

配置上述其他操作。

更新更新

主要问题是在注册表单中,因为它没有正确使用表单构建器而且没有发布params。我将before_filter :configure_permitted_parameters, if: :devise_controller? def configure_permitted_parameters devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:email, :password, :password_confirmation) } devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:email, :password, :password_confirmation) } end 文件修改为:

app/views/devise/registrations/new.html.erb

确保params正确地发布到控制器,现在一切正常。

答案 1 :(得分:0)

我能看到的,以及你解释的内容:

  1. you filled in the email and password field

  2. it went through your controller and then to the model

  3. there is validation in your model that is complaining of the absence of email and password

  4. 我假设您正在使用Devise生成的注册页面(这已经设置为获取新的用户表单,绑定了User类的新实例)。 < / p>

    从以上三点(以及假设)来看,您填写的电子邮件和密码都会在途中丢失。

    我的猜测是,这会发生在控制器的某个地方。根据您的路线,这将是注册控制器(create action)。

    您是否拥有自定义registrations#create控制器?如果是=&gt;我认为您的参数不允许构建正在调用save操作的新用户。

    如果是这种情况,您将需要以下内容:

    def new_user_params
      params.require(:user).permit(:email, :password, :password_confirmation)
    end
    

    然后构建并将新用户保存为:

    @user = User.new(new_user_params)
    @user.save
    

    希望这有助于为它提供一些启示......