Ken
Ken

Reputation: 184

No route matches [POST] "/users/1/edit" when updating from form - Rails

New to Rails..

I'm accessing a form at: /users/1/edit (which shows but doesn't update and throws error) and I also have a devise form at: /user/edit (which shows and updates correctly)

I'm using Rails 4 and Devise 3.1.

routes.rb:

    resources :users
    devise_for :user, :controllers => { registrations: 'registrations' }
    get 'users/:id' => 'users#show'

and my available paths:

users_path                      GET /users(.:format)    users#index
                                POST    /users(.:format)    users#create
new_user_path                   GET /users/new(.:format)    users#new
edit_user_path                  GET /users/:id/edit(.:format)   users#edit
user_path                       GET /users/:id(.:format)    users#show
                                PATCH   /users/:id(.:format)    users#update
                                PUT /users/:id(.:format)    users#update
                                DELETE  /users/:id(.:format)    users#destroy

new_user_session_path           GET /user/sign_in(.:format) devise/sessions#new
user_session_path               POST    /user/sign_in(.:format) devise/sessions#create
destroy_user_session_path       DELETE  /user/sign_out(.:format)    devise/sessions#destroy
user_password_path              POST    /user/password(.:format)    devise/passwords#create
new_user_password_path          GET /user/password/new(.:format)    devise/passwords#new
edit_user_password_path         GET /user/password/edit(.:format)   devise/passwords#edit
                                PATCH   /user/password(.:format)    devise/passwords#update
                                PUT /user/password(.:format)    devise/passwords#update

cancel_user_registration_path   GET /user/cancel(.:format)  registrations#cancel
user_registration_path          POST    /user(.:format) registrations#create
new_user_registration_path      GET /user/sign_up(.:format) registrations#new
edit_user_registration_path     GET /user/edit(.:format)    registrations#edit
                                PATCH   /user(.:format) registrations#update
                                PUT /user(.:format) registrations#update
                                DELETE  /user(.:format) registrations#destroy
                                GET /users/:id(.:format)    users#show

users_controller.rb

    def index
     @users = User.all
    end

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

    def edit
    end

    def create
     @user = User.create(user_params)
    end

    def destroy
    end

    private

    def user_params
     params.require(:user).permit(:avatar)
    end

UPDATE: I figured out an answer for this so removed some of the content.

Upvotes: 1

Views: 1827

Answers (2)

ALLCAPZ
ALLCAPZ

Reputation: 81

I'm a little confused by how you setup your application but it might be worth noting that I don't think your form knows what to update. try this:

<%= form_for @profile do |f| %>
...

Then you need to update your edit action in your User Controller:

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

Please note it can sound like Profile is its own class and belongs to the User because your user model states "has_one :profile." But I don't think that's actually the case and if its not, you should remove that language from your model. But if it is the case, you'll need to have a profile controller that will have similar CRUD actions which should include the action method I described above.

Upvotes: 1

Anthony
Anthony

Reputation: 15957

Your Users controller needs an update action

class UsersController < ApplicationController

  ...

  def update
    user = User.find(params[:id])
    user.update(user_params)
  end
end

Upvotes: 0

Related Questions