繁体   English   中英

Devise - 未显示错误消息 - 重定向到更新而不是创建

[英]Devise - Error message not showing up - redirects to update instead of create

我已经重写了注册 controller 并且我正在尝试返回表单以创建用户,但它只能部分工作。

def create
  u = User.find_by_email(params[:user][:email])
  ...
  if u && u.unregistered
    self.resource = u
    if resource.update_with_password(params[resource_name])
      ...
      set_flash_message :notice, :updated if is_navigational_format?
      ...
      sign_in(resource_name, resource)
      if u.confirmed?
        redirect_to consultations_path
      else
        redirect_to send_advisor_confirmation_path(u.id)
      end

    else
      clean_up_passwords(resource)
      # this is the place that has a problem
      respond_with_navigational(resource) { render_with_scope :new }
    end
    return
  end
  super
end

(旁注:在我的应用程序中,可以创建一个尚未登录的用户,他们被定义为“未注册”,他们可以通过注册过程来申请他们的帐户)。

这个 respond_with_navigational 第一次工作(当它是new发布到create时)但是当你再次弄乱表单时它会清除整个表单(当create发布到create ...或者应该是)。 日志说它第一次要创建,但第二次要更新:

Started POST "/users" for 127.0.0.1 at 2011-07-20 15:49:30 -0500
  Processing by RegistrationsController#create as HTML
...
Started POST "/users" for 127.0.0.1 at 2011-07-20 15:50:56 -0500
  Processing by RegistrationsController#update as HTML

根据路线(耙路线):

          POST   /users(.:format)             {:action=>"create", :controller=>"users"}
 new_user GET    /users/new(.:format)         {:action=>"new", :controller=>"users"}
edit_user GET    /users/:id/edit(.:format)    {:action=>"edit", :controller=>"users"}
     user GET    /users/:id(.:format)         {:action=>"show", :controller=>"users"}
          PUT    /users/:id(.:format)         {:action=>"update", :controller=>"users"}

那么为什么将发送帖子定向到更新? 我该如何解决这个问题?

更新:好的,所以它被重定向到更新(我相信),因为资源被定义为现有的 object。 但是因为我有 devise 的自定义策略说这个特定的用户没有经过身份验证,所以它无法 go 更新(需要身份验证)并被重定向到新的,但没有参数。

所以问题就变成了,我如何设置资源使它看起来像一个新用户,但是在创建新用户之前它有错误。

例如,错误可能是“密码不匹配”,但如果您在新用户身上发现错误,则可能是“电子邮件已被占用”。

编辑1:我错过了你的一些问题,这可能没有帮助。

确保你使用这样的东西在你的页面上显示它:

layouts/application.html.erb(或任何你需要的地方)

<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>

我不喜欢这个,但它有效。 如果有人有更好的方法,请告诉我。 谢谢你。

...
else
  resource.unregistered_advisor = true
  clean_up_passwords(resource)
  a = User.new(:email => resource.email, :name => resource.name, :confirmation_token => resource.confirmation_token)
  resource.errors.each do |key,value|
    a.errors[key] = value
  end
  self.resource = a
  respond_with_navigational(resource) { render_with_scope :new }
end
...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM