

初めての質問なので説明不足などあるかもと思うのですが、アドバイスお願いします。

railsチュートリアルベースでプログラム組んでいて、４章と９章を飛ばしてしまっているのですが、一応１０章まで組んで、その上にオリジナルのプログラムを乗っけている感じです。

本題なのですが作成したユーザーのeditをsaveしようとすると `No route matches [PATCH] "/users/7/edit"`
というエラーが出てしまいます。

* * *

edit.html.erb

[code]

    <% provide(:title, 'Edit user') %>
    <% provide(:button_text, 'Save changes') %>
    <h1>Update your profile</h1>
    <div class="row">
      <div class="col-md-6 col-md-offset-3">
        <%= render 'form' %>
        <div class="gravatar_edit">
          <%= gravatar_for @user %>
          <a href="http://gravatar.com/emails" target="_blank">Change</a>
        </div>
      </div>
    </div>
    
[/code]

_fome.html.erb

[code]

    <%= form_for(@user, url: yield(:url)) do |f| %>
      <%= render 'shared/error_messages', object: f.object %>
      <%= f.label :name %>
      <%= f.text_field :name, class: 'form-control' %>
      <%= f.label :email %>
      <%= f.email_field :email, class: 'form-control' %>
      <%= f.label :password %>
      <%= f.password_field :password, class: 'form-control' %>
      <%= f.label :password_confirmation, "Confirmation" %>
      <%= f.password_field :password_confirmation, class: 'form-control' %>
      <%= f.submit yield(:button_text), class: "btn btn-primary" %>
    <% end %>
    
[/code]

routes.rb

[code]

    Rails.application.routes.draw do
      get 'sessions/new'
    
      get 'users/new'
    
      root 'static_pages#home'
      get  '/help',    to: 'static_pages#help'
      get  '/about',   to: 'static_pages#about'
      get  '/contact', to: 'static_pages#contact'
      get  '/signup',  to: 'users#new'
      post '/signup',  to: 'users#create'
      get    '/login',   to: 'sessions#new'
      post   '/login',   to: 'sessions#create'
      delete '/logout',  to: 'sessions#destroy'
      get '/unity', to: 'static_pages#unity'
      resources :users
    
      # Place リソースのルーティング
      resources :places
    end
    
[/code]

user_controller.rb

[code]

    class UsersController < ApplicationController
      before_action :logged_in_user, only: [:index, :edit, :update, :destroy]
      before_action :logged_in_user, only: [:edit, :update]
      before_action :admin_user,     only: :destroy
    
      def index
        @users = User.paginate(page: params[:page])
      end
    
      def show
        @user = User.find(params[:id])
        @places = @user.place.paginate(page: params[:page])
      end
    
      def new
        @user = User.new
      end
    
      def create
        @user = User.new(user_params)
        if @user.save
          log_in @user
          flash[:success] = "Welcome to the Sample App!"
          redirect_to @user
        else
          render 'new'
        end
      end
    
      def edit
        @user = User.find(params[:id])
      end
    
      def update
        @user = User.find(params[:id])
        if @user.update_attributes(user_params)
          flash[:success] = "Profile updated"
          redirect_to @user
          # 更新に成功した場合を扱う。
        else
          render 'edit'
        end
      end
    
      def destroy
        User.find(params[:id]).destroy
        flash[:success] = "User deleted"
        redirect_to users_url
      end
    
      private
    
      def user_params
        params.require(:user).permit(:name, :email, :password,
                                   :password_confirmation)
      end
    
      # beforeアクション
    
      # 正しいユーザーかどうか確認
      def correct_user
        @user = User.find(params[:id])
        redirect_to(root_url) unless current_user?(@user)
      end
    
      # 管理者かどうか確認
      def admin_user
        redirect_to(root_url) unless current_user.admin?
        end
    end
    
[/code]

現在のルーティング出力

[code]

    $ bundle exec rake routes
    
      Prefix Verb   URI Pattern                Controller#Action
      sessions_new GET    /sessions/new(.:format)    sessions#new
      users_new    GET    /users/new(.:format)       users#new
      root         GET    /                          static_pages#home
      help         GET    /help(.:format)            static_pages#help
      about        GET    /about(.:format)           static_pages#about
      contact      GET    /contact(.:format)         static_pages#contact
      unity        GET    /unity(.:format)           static_pages#unity
      signup       GET    /signup(.:format)          users#new
                   POST   /signup(.:format)          users#create
      edit         GET    /edit(.:format)            users#edit
      login        GET    /login(.:format)           sessions#new
                   POST   /login(.:format)           sessions#create
      logout       DELETE /logout(.:format)          sessions#destroy
      users        GET    /users(.:format)           users#index
                   POST   /users(.:format)           users#create
      new_user     GET    /users/new(.:format)       users#new
      edit_user    GET    /users/:id/edit(.:format)  users#edit
      user         GET    /users/:id(.:format)       users#show
                   PATCH  /users/:id(.:format)       users#update
                   PUT    /users/:id(.:format)       users#update
                   DELETE /users/:id(.:format)       users#destroy
      places       GET    /places(.:format)          places#index
                   POST   /places(.:format)          places#create
      new_place    GET    /places/new(.:format)      places#new
      edit_place   GET    /places/:id/edit(.:format) places#edit
      place        GET    /places/:id(.:format)      places#show
                   PATCH  /places/:id(.:format)      places#update
                   PUT    /places/:id(.:format)      places#update
                   DELETE /places/:id(.:format)      places#destroy
    
[/code]


CC BY-SA 4.0
2018-11-30T06:11:41.757
50772
2021-06-26T05:05:00.613
2020-12-07T02:08:33.883
3060
31218
question
ruby-on-rails
ユーザーeditでNo route matches [PATCH] "/users/7/edit"になってしまいます


自己解決できました。  
_fome.html.erb

[code]

    <%= form_for(@user, url: yield(:url)) do |f| %>
    
[/code]

を ↓

[code]

    <%= form_for(@user, url: user_path) do |f| %>
    
[/code]

に変換したら通りました。

参考にしたリンクは↓です。  
<https://teratail.com/questions/66002>

相談に乗ってくれた方へ、ありがとうございました。


CC BY-SA 4.0
2018-11-30T08:17:52.050
50780
2018-11-30T08:17:52.050
31218
50772
answer