brg
brg

Reputation: 3953

rails-4 devise + user controller edit route redirecting to the wrong place

I am using devise gem in my app and a custom user controller. I have links to edit a user in both the show and index page. After signing in, if I click the edit link in users/index.html.erb or users/edit.html.erb, it redirects to user new form even though a GET request was sent.

In the route, I am skipping registrable because I am using the User controller to handle that and it works fine.

this is the route

devise_for :users, :path_prefix => 'd', skip: [:registrations], controllers: {sessons: "sessions"}
resources :users

rake routes | grep edit_user gives

  edit_user_password GET    /d/users/password/edit(.:format) devise/passwords#edit                                                                
  edit_user GET    /users/:id/edit(.:format)        users#edit

users/show.html.erb

 <div class="btn btn-primary"> <%= link_to "Edit", edit_user_path(@user) %></div>

This is the screenshot of the registration form that comes up when I click on the edit link in either the index or show page. It is making a get request but displaying the form for new instead of form for edit.

enter image description here

The is the log from clicking the edit link in the show page:

  Started GET "/users/17/edit" for at 2013-08-19 08:40:49 +0000                                                                       
    Processing by UsersController#edit as HTML                                                                                                        
    Parameters: {"id"=>"17"}                                                                                                                        
    User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", "17"]]                                                 
    User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 17 ORDER BY "users"."id" ASC LIMIT 1                                      
    Rendered users/_form.html.erb (17.5ms)                                                                                                          
    Rendered users/edit.html.erb within layouts/application (26.8ms)                                                                                
    Completed 200 OK in 50ms (Views: 47.1ms | ActiveRecord: 0.5ms

The users_controller.rb

class UsersController < ApplicationController
   before_action :set_user, only: [:show, :edit, :update, :destroy]
   before_filter :authenticate_user!, except: [:new, :create]
   respond_to :html, :json

   def show
   end

   def edit
   end

   private

   def set_user
      @user = User.find(params[:id])
   end

 end

users/_edit.html.erb I even specified url path in the form and it still renders new form

 form_for(@user, :url => edit_user_path(@user))

users/edit.html.erb

  <%= render "edit" %>

users/index.html.erb

 <% @users.each do |user| %>

  <div class="btn btn-primary"><%= link_to "Edit", edit_user_path(user) %> </div>
  <div class="btn btn-danger"><%= link_to 'Remove', user, method: :delete, data:    {confirmation: 'Are you sure'} %></div>
  <% end %>

Upvotes: 0

Views: 614

Answers (1)

Justin D.
Justin D.

Reputation: 4976

You are making a POST request to the edit user path whereas this path is registered with the GET method. That's why you get this error.

Personally, instead of skipping the registration, I would have overwritten the registrations controller as described in the Devise wiki.

Upvotes: 2

Related Questions