Rails 4 How Overwrite Devise Respond Path Upon Error

后端 未结 3 2103
说谎
说谎 2021-02-13 09:38

I\'ve been struggling for a while with this one. I have a Rails4/Devise 3.1 app with two users in the system:

  1. Graduates
  2. Employers

and o

相关标签:
3条回答
  • 2021-02-13 10:25

    Apparently, the :location parameter is ignored if you have an existing template called, well, in this case, #index. I still don't understand why Devise redirects to #index or #show when the resource has errors on it.

    I'll update this answer as I find out more.

    0 讨论(0)
  • 2021-02-13 10:27

    Calling respond_with will render the default action of that resource. This I believe is the index action (/users/) and thus the cause for the redirect.

    I think what you want to do is render the new action instead. Try the following:

    if resource.save
      ...
    else
      clean_up_passwords(resource)
      render :action => 'new'
    end
    
    0 讨论(0)
  • 2021-02-13 10:30

    In case of respond_with, for an html response - if the request method is get, an exception is raised but for other requests such as post the response depends on whether the resource has any validation errors (i.e. assuming that an attempt has been made to save the resource, e.g. by a create action) -

    If there are no errors, i.e. the resource was saved successfully, the response redirect's to the resource i.e. its show action.

    If there are validation errors, the response renders a default action, which is :new for a post request or :edit for patch or put.

    source: http://apidock.com/rails/v4.1.8/ActionController/MimeResponds/respond_with

    For your case, something like render 'view_path' in place of respond_with block should work.

    0 讨论(0)
提交回复
热议问题