###missing required keys: [:id]というエラーで困っています。
発生している問題・エラーメッセージ
ActionController::UrlGenerationError in StaticPages#home Showing /home/ec2-user/environment/matching_app/app/views/layouts/_header.html.erb where line #9 raised: No route matches {:action=>"show", :controller=>"accounts"}, missing required keys: [:id]
該当のソースコード
_header.html.erb
<header class="navbar navbar-fixed-top navbar-inverse"> <div class ="container"> <%= link_to "matching app", root_path, id: "logo" %> <% if account_signed_in? %> <nav> <ul class="nav navbar-nav navbar-right"> <li><%=link_to '使い方', '/howto' %></li> <li><%=link_to '友達一覧', '/accounts/index' %></li> <li><%=link_to '自分のプロフィール', profile_path %></li> <li><%=link_to '設定', edit_account_registration_path %></li> <li><%=link_to 'ログアウト', destroy_account_session_path, method: :delete %></li> </ul> </nav> <% else %> <nav> <ul class="nav navbar-nav navbar-right"> <li><%=link_to 'アカウント作成', new_account_registration_path %></li> <li><%=link_to 'ログイン', new_account_session_path %></li> </ul> </nav> <% end %> </div> </header> <% if account_signed_in? %> <div class="status-message" > <p>XXXXXX</p> </div> <% end %>
accounts_controller.erb
class AccountsController < ApplicationController def index @accounts = Account.all end def show @account = Account.find(params[:id]) end end
routes.rb
Rails.application.routes.draw do devise_for :accounts get 'static_pages/home' # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html root "static_pages#home" get "/home" => "static_pages#home" get "/howto" => "static_pages#howto" get "/accounts/sign_up" => "devise/registrations#new" get "/accounts/index" => "accounts#index" get "/accounts/profile/:id", to: "accounts#show", as: "profile" resources :accounts end
account.rb
class Account < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable has_many :active_relationships, class_name: "Relationship", foreign_key: "follower_id", dependent: :destroy end
application_record.rb
class ApplicationRecord < ActiveRecord::Base self.abstract_class = true end
rake routesをすると...
Controller#Action new_account_session GET /accounts/sign_in(.:format) devise/sessions#new account_session POST /accounts/sign_in(.:format) devise/sessions#create destroy_account_session DELETE /accounts/sign_out(.:format) devise/sessions#destroy new_account_password GET /accounts/password/new(.:format) devise/passwords#new edit_account_password GET /accounts/password/edit(.:format) devise/passwords#edit account_password PATCH /accounts/password(.:format) devise/passwords#update PUT /accounts/password(.:format) devise/passwords#update POST /accounts/password(.:format) devise/passwords#create cancel_account_registration GET /accounts/cancel(.:format) devise/registrations#cancel new_account_registration GET /accounts/sign_up(.:format) devise/registrations#new edit_account_registration GET /accounts/edit(.:format) devise/registrations#edit account_registration PATCH /accounts(.:format) devise/registrations#update PUT /accounts(.:format) devise/registrations#update DELETE /accounts(.:format) devise/registrations#destroy POST /accounts(.:format) devise/registrations#create static_pages_home GET /static_pages/home(.:format) static_pages#home root GET / static_pages#home home GET /home(.:format) static_pages#home howto GET /howto(.:format) static_pages#howto accounts_sign_up GET /accounts/sign_up(.:format) devise/registrations#new accounts_index GET /accounts/index(.:format) accounts#index profile GET /accounts/profile/:id(.:format) accounts#show accounts GET /accounts(.:format) accounts#index POST /accounts(.:format) accounts#create new_account GET /accounts/new(.:format) accounts#new edit_account GET /accounts/:id/edit(.:format) accounts#edit account GET /accounts/:id(.:format) accounts#show PATCH /accounts/:id(.:format) accounts#update PUT /accounts/:id(.:format) accounts#update DELETE /accounts/:id(.:format) accounts#destroy
railsを勉強して自分のアプリを作ろうとした時に起きた問題です。
「idが渡されていない」という事が問題だと思うのですが、具体的に自分がどのようにしてコードを書くべきなのかが良くわからなかったため投稿させていただきました。
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2020/01/13 22:28
2020/01/14 00:30
退会済みユーザー
2020/01/14 06:46
2020/01/14 11:23
退会済みユーザー
2020/01/17 00:19
2020/01/17 00:31
2020/01/17 00:39
2020/01/17 00:44
退会済みユーザー
2020/01/17 01:27