登録しているユーザーを一覧表示するページを作っています。
以下のようなエラーが発生して困っています。
色々と調べたのですが解決できなかったため見て頂けると嬉しいです。
ActionController::UrlGenerationError in Users#index Showing /home/ec2-user/environment/bookers-level2.herokuapp/app/views/users/index.html.erb where line #20 raised: No route matches {:action=>"edit", :controller=>"users"} missing required keys: [:id] Extracted source (around line #20): 18 </tr> 19 </table> 20 <%= link_to edit_user_path, class: "btn btn-default" do %> ←エラー箇所 21 <i class="fa fa-wrench"></i> 22 <% end %> 23 <p>New book</p>
以下必要そうなコードになります。
users_controller
class UsersController < ApplicationController def index @user = current_user @users = User.all end def show @user = User.find(params[:id]) @book = Book.new @books = @user.books end def edit @user = User.find(params[:id]) end private def user_params params.require(:user).permit(:name, :introduction, :profile_image_id) end end
index.html.erb
<%= render 'shared/header' %> <div class="top"> <div class="container"> <div class="row"> <div class="col-lg-3"> <p>User info</p> <%= attachment_image_tag @user, :profile_image_id, :fill, 250, 250, format: 'jpeg', fallback: "no_image.jpg", size:'250x250' %> <table class="table"> <tr> <th>name</th> <th><%= current_user.name %></th> </tr> <tr> <th>introduction</th> <th><%= current_user.introduction %></th> </tr> </table> <%= link_to edit_user_path, class: "btn btn-default" do %> <i class="fa fa-wrench"></i> <% end %> <p>New book</p> <%= form_for(@book, url: '/users') do |f| %> <p>Title</p> <%= f.text_field :title %> <p>Opinion</p> <%= f.text_area :body %> <%= f.submit 'Create Book' %> <% end %> </div> <div class="col-lg-9"> <p>Books</p> <table class="table table-hover"> <thead> <tr> <th></th> <th>Title</th> <th>Opinion</th> </tr> </thead> <tbody> <% if @users.present? %> <% @users.each do |user| %> <tr> <td></td> <td><%= user.title %></td> <td><%= user.opinion %></td> </tr> <% end %> <% end %> </tbody> </table> </div> </div> </div> </div> <%= render 'shared/footer' %>
routes.rb
Rails.application.routes.draw do devise_for :users get root 'top#top' get '/home/about', to: 'top#home' resources :users resources :books # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html end
$ rake routes
Prefix Verb URI Pattern Controller#Action new_user_session GET /users/sign_in(.:format) devise/sessions#new user_session POST /users/sign_in(.:format) devise/sessions#create destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy new_user_password GET /users/password/new(.:format) devise/passwords#new edit_user_password GET /users/password/edit(.:format) devise/passwords#edit user_password PATCH /users/password(.:format) devise/passwords#update PUT /users/password(.:format) devise/passwords#update POST /users/password(.:format) devise/passwords#create cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel new_user_registration GET /users/sign_up(.:format) devise/registrations#new edit_user_registration GET /users/edit(.:format) devise/registrations#edit user_registration PATCH /users(.:format) devise/registrations#update PUT /users(.:format) devise/registrations#update DELETE /users(.:format) devise/registrations#destroy POST /users(.:format) devise/registrations#create root GET / top#top home_about GET /home/about(.:format) top#home 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 books GET /books(.:format) books#index POST /books(.:format) books#create new_book GET /books/new(.:format) books#new edit_book GET /books/:id/edit(.:format) books#edit book GET /books/:id(.:format) books#show PATCH /books/:id(.:format) books#update PUT /books/:id(.:format) books#update DELETE /books/:id(.:format) books#destroy refile_app /attachments #<Refile::App app_file="/home/ec2-user/.rvm/gems/ruby-2.6.3/bundler/gems/refile-46b4178654e6/lib/refile/app.rb">
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/11/17 05:00
退会済みユーザー
2019/11/17 06:10
退会済みユーザー
2019/11/17 13:38 編集