开发者

Redirecting when editing/updating a user (with error)

开发者 https://www.devze.com 2023-03-03 21:54 出处:网络
I want to update/edit a devise user from my own form in my project, but the problem is that I can\'t redirect on update on the \"request.referer\".

I want to update/edit a devise user from my own form in my project, but the problem is that I can't redirect on update on the "request.referer".

I've read that but it didn't work for me: https://github.com/plataformatec/devise/wiki/How-To:-Customize-the-redirect-after-a-user-edits-their-profile ...And the others wiki pages.

Okay, so my code is:

#/views/backend/perso.html.erb
<%= form_for(@user, :url => registration_path(@user), :html => { :method => :put }) do |f| %>
  <% if @user.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(form.errors.count, "error") %> prohibited this data from being saved:</h2>

      <ul>
      <% @user.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
# Other fields ...
  <div class="field">
    <%= f.label :email %><br />
    <%= f.email_field :email %>
  </div>

  <div class="field">
    <%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
    <%= f.password_field :current_password %>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

So the user is on that page, and updates his data. Problem, I get redirected to /users/ I've tried to add that to routes:

#config/routes.rb
devise_for :users do
    get "users", :to => "backend#perso", :as => :user_root # Rails 3
end

Or even to the application controller:

#/controllers/application_controller.rb
private
def after_update_pa开发者_StackOverflowth_for(resource)
  backend_perso_path
end

But still not working.

Thanks for anyone trying to help me out !

Edit

When the update generates no error, I get redirected to the page I want (by adding after_update_path_for in my application controller), but when errors exist, it displays /view/devise/registration/edit.html.erb

Update

Ok, so I overwrited the Devise controller following that: https://github.com/plataformatec/devise/wiki/How-To%3a-Allow-users-to-edit-their-account-without-providing-a-password

So my code in registrations_controller looks like that

#controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
  def update
    # Devise use update_with_password instead of update_attributes.
    # This is the only change we make.
    if resource.update_attributes(params[resource_name])
      set_flash_message :notice, :updated
      # Line below required if using Devise >= 1.2.0
      sign_in resource_name, resource, :bypass => true
      redirect_to after_update_path_for(resource)
    else
      clean_up_passwords(resource)
      redirect_to backend_perso_path # That's the line I need to change
    end
  end
end

I can now redirect to the page I wanted, but I don't know how to show that errors happened !


simple answer via Rails Routing from the Outside In using the "not quite as elegant" hardcoded user_root as described in How To: Customize the redirect after a user edits their profile

#config/routes.rb
match 'user_root' => redirect("/wherever/you/want")


I didn't get any useful answer (thanks Laurent for your solution though!) so here's my code so far.

Do the job but isn't very clean.

class RegistrationsController < Devise::RegistrationsController
  def update
    # Devise use update_with_password instead of update_attributes.
    # This is the only change we make.
    if resource.update_attributes(params[resource_name])
      set_flash_message :notice, :updated
      # Line below required if using Devise >= 1.2.0
      sign_in resource_name, resource, :bypass => true
      redirect_to after_update_path_for(resource)
    else
      clean_up_passwords(resource)
      redirect_to after_update_path_for(resource), :flash => { :alert => "Error message" }
    end
  end

  private
  # Redirect to the URL after modifying Devise resource (Here, our user)
  def after_update_path_for(resource)
    my_path
  end
end


I don't use devise, but I've been encountering a similar problem on omniauth. OmniAuth has a referer or origin method, but it wasn't working for me for account creation. So what I did is a dirty hack storing the source page in a session variable, and checking it after the action was performed.

0

精彩评论

暂无评论...
验证码 换一张
取 消