質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

7702閲覧

missing required keys: [:id]というエラーで困っています。

退会済みユーザー

退会済みユーザー

総合スコア0

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/01/13 10:34

編集2020/01/13 22:36

###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が渡されていない」という事が問題だと思うのですが、具体的に自分がどのようにしてコードを書くべきなのかが良くわからなかったため投稿させていただきました。
よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

profile_path
edit_account_registration_path

といった単数形の path はある特定の Profile、AccountRegistration を操作するものです。
ですので、これらは 特定 のためのパラメータが必要です。

モデルの定義が載っていないので推定ですが、どのprofileかというと、current_userのprofileかと思います。すると
profile_path(current_user.profile) とするのかな。

edit_account_registration_path ですが、これ routesにあります?
accountsの情報の修正だと思うのですが、、、
edit_account_path(current_user.profile) なのでは?

投稿2020/01/13 11:40

winterboum

総合スコア23284

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2020/01/13 22:28

ルーティングで as: "profile"と定義するとprofile_pathとして使えるみたいな事をある動画で言っていたので使ってみました。その時、モデルには何もprofileについて記述していません。また、edit_account_registration_pathはdevise gemを入れたため動いているのだと思います。 一応モデルも貼っておきます。よろしくお願いします。
winterboum

2020/01/14 00:30

as: "profile"と定義するとprofile_pathとして使えます。 が、それは必要な引数については影響を与えません。必要なものは必要なのdす profile GET /accounts/profile/:id(.:format) となってますね。:idが必要です
退会済みユーザー

退会済みユーザー

2020/01/14 06:46

profile_path(1)とするとidが1の人の名前やメールアドレスが出て、実際機能しますけど、ログインしている人の名前とメールアドレスを表示させるにはどうしたら良いか良くわからないです。ポンコツでごめんなさい。
winterboum

2020/01/14 11:23

loginしている人は大抵は current_user で参照できますが、そうなってますか? なっていたら <li><%=link_to '自分のプロフィール', profile_path current_user%></li> で
退会済みユーザー

退会済みユーザー

2020/01/17 00:19

ごめんなさい、できませんでした。他にも色々試しましたがうまくいかなかったため、勉強し直して見ます。ありがとうございました。
winterboum

2020/01/17 00:31

loginのところのプログラムを見てください
Mugheart

2020/01/17 00:39

deviseを使っている想定で話しますが モデル名が Account なので current_user ではなく current_account では?
Mugheart

2020/01/17 00:44

<li><%=link_to '自分のプロフィール', profile_path(id: current_account.id) %></li>
退会済みユーザー

退会済みユーザー

2020/01/17 01:27

お二方ありがとうございました!できました。ちなみにMugheartさんのコードで上手くいきました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問