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

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

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

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

Ruby on Rails

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

Q&A

解決済

1回答

6161閲覧

RubyOnRails ルーティングエラーとなり、正しく表示がされない

webillion

総合スコア33

Ruby

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

Ruby on Rails

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

0グッド

0クリップ

投稿2017/04/04 01:23

###前提・実現したいこと

教本(RubyOnRails5超入門)に従いながら、RubyOnRailsで読書カードを作っています。「localhost:3000/cards/1」とURLを叩くと、読書カードが表示されるはずなのですが、エラーになってしまいます。
showアクションを実装中に以下のエラーメッセージが発生しました。
分かる方、アドバイス願います。下記、エラー文と追加したコードになります。
###発生している問題・エラーメッセージ

Routing Error No route matches [GET] "/cards" Rails.root: C:/Users/daino/Desktop/RailsApp Application Trace | Framework Trace | Full Trace Routes Routes match in priority from top to bottom Helper HTTP Verb Path Controller#Action Path / Url Path Match cards_index_path GET /cards/index(.:format) cards#index cards_show_path GET /cards/show(.:format) cards#show cards_add_path GET /cards/add(.:format) cards#add cards_edit_path GET /cards/edit(.:format) cards#edit GET /people/delete/:id(.:format) people#delete GET /people/edit/:id(.:format) people#edit POST /people/edit/:id(.:format) people#update people_index_path GET /people/index(.:format) people#index people_path GET /people(.:format) people#index people_add_path GET /people/add(.:format) people#add POST /people/add(.:format) people#create GET /people/:id(.:format) people#show helo_path POST /helo(.:format) helo#index helo_index_path POST /helo/index(.:format) helo#index helo_other_path GET /helo/other(.:format) helo#other GET /helo/index(.:format) helo#index GET /helo(.:format) helo#index Request Parameters: None Toggle session dump Toggle env dump Response Headers: None

###該当のソースコード
20170403090910_create_cards.rb

class CreateCards < ActiveRecord::Migration[5.0] def change create_table :cards do |t| t.text :title t.text :author t.integer :price t.text :publisher t.text :memo t.timestamps end end end

cards_controller.rb

class CardsController < ApplicationController layout 'cards' def index @cards = Card.all end def show @card = Card.find(params[:id]) end def add if request.post? then Card.create(card_params) goback else @card = Card.new end end def edit @card = Card.find(params[:id]) if request.patch? then @card.update(card_params) goback end end def delete Card.find(params[:id]).destroy goback end private def card_params params.require(:card).permit(:title, :author, :price, :publisher, :memo) end private def goback redirect_to '/cards' end end

index.html.erb

<h1>Cards#index</h1> <p>※現在、登録されているデータの一覧です。</p> <table width="100%"> <tr> <th>ID</th><th>題名</th> <th>著者</th><th colspan="2"></th> </tr> <%= @cards.each do |obj| %> <tr> <td width="35px"><%= obj.id %></td> <td width="50px"><a href="/cards/<% = obj.id %>"><%= obj.title %></a></td> <td><%= obj.author %></td> <td width="40px"><a href="/cards/edit/<%= obj.id %>">編集</a></td> <td width="40px"><a href ="javascript:window.delData(<%= (obj.id.to_s + ",'".to_s + obj.title.to_s).html_safe %>');">削除</a></td> </tr> <% end %> </table> <p class ="link"><a href="/cards/add">※新しいデータを入力 &gt;&gt;</a></p>

###試したこと
・Routingの確認
・書き写しの抜け漏れチェック

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

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

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

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

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

guest

回答1

0

ベストアンサー

cards_show_path GET /cards/show(.:format) cards#show

ですが、この設定だと/cards/showというurlでないとcardsControllerのshowアクションに行きません

正しくは

cards_show_path GET /cards/:id(.:format) cards#show

ではないでしょうか。
idを受け取るようにしましょう!

投稿2017/04/04 02:02

MasakazuFukami

総合スコア1869

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

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

moke

2017/04/05 00:55

別のところにありません。 MasakazuFukami様の言っていることはrouteの設定が間違っていますよと言うことです。 route.rbの記述を上記のように変更しましょうと言う意味です。
MasakazuFukami

2017/04/05 02:19

実際にid=1のデータは存在しますか? cardテーブルの中にid=1のレコードはありますでしょうか?
webillion

2017/04/05 02:24

確認の仕方はどのような手順を踏めば良いでしょうか?
MasakazuFukami

2017/04/05 02:32

一番簡単なのはデータベースを見ることが出来るソフトを入れるのが簡単です。 sequel proなど もしそれがない場合は、terminalを開いて、railsプロジェクトのrootディレクトリまで行ってください。 そこで rails c を押し、 Card.all で現在登録されてあるcardテーブルの中身が全て見れます それをココに貼って下さい
webillion

2017/04/05 05:42

コマンドプロンプトでの実行結果です。やり方が違うのでしょうか? C:\Users\daino\Desktop\RailsApp>rails c C:/Users/daino/Desktop/RailsApp/config/routes.rb:12:in `block in <top (required)>': undefined method `parch' for #<ActionDispatch::Routing::Mapper:0x0000000427d630> (NoMethodError) Did you mean? patch from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/routing/route_set.rb:389:in `instance_exec' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/routing/route_set.rb:389:in `eval_block' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/routing/route_set.rb:371:in `draw' from C:/Users/daino/Desktop/RailsApp/config/routes.rb:1:in `<top (required)>' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:287:in `load' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:287:in `block in load' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:259:in `load_dependency' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:287:in `load' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.0.2/lib/rails/application/routes_reloader.rb:40:in `block in load_paths' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.0.2/lib/rails/application/routes_reloader.rb:40:in `each' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.0.2/lib/rails/application/routes_reloader.rb:40:in `load_paths' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.0.2/lib/rails/application/routes_reloader.rb:16:in `reload!' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.0.2/lib/rails/application/routes_reloader.rb:26:in `block in updater' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activesupport-5.0.2/lib/active_support/file_update_checker.rb:77:in `execute' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.0.2/lib/rails/application/routes_reloader.rb:27:in `updater' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.0.2/lib/rails/application/routes_reloader.rb:7:in `execute_if_updated' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.0.2/lib/rails/application/finisher.rb:119:in `block in <module:Finisher>' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.0.2/lib/rails/initializable.rb:30:in `instance_exec' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.0.2/lib/rails/initializable.rb:30:in `run' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.0.2/lib/rails/initializable.rb:55:in `block in run_initializers' from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:228:in `block in tsort_each' from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component' from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:431:in `each_strongly_connected_component_from' from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:349:in `block in each_strongly_connected_component' from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:347:in `each' from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:347:in `call' from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:347:in `each_strongly_connected_component' from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:226:in `tsort_each' from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:205:in `tsort_each' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.0.2/lib/rails/initializable.rb:54:in `run_initializers' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.0.2/lib/rails/application.rb:352:in `initialize!' from C:/Users/daino/Desktop/RailsApp/config/environment.rb:5:in `<top (required)>' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.0.2/lib/rails/application.rb:328:in `require' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.0.2/lib/rails/application.rb:328:in `require_environment!' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.0.2/lib/rails/commands/commands_tasks.rb:157:in `require_application_and_environment!' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.0.2/lib/rails/commands/commands_tasks.rb:77:in `console' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.0.2/lib/rails/commands/commands_tasks.rb:49:in `run_command!' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.0.2/lib/rails/commands.rb:18:in `<top (required)>' from bin/rails:4:in `require' from bin/rails:4:in `<main>' C:\Users\daino\Desktop\RailsApp>card.all 'card.all' は、内部コマンドまたは外部コマンド、 操作可能なプログラムまたはバッチ ファイルとして認識されていません。
moke

2017/04/05 05:49

rails c が失敗してますね route.rb内のpatchと書くべきところをparchにしているようです。 それを修正して再度 rails c してください
moke

2017/04/05 05:51

rails cが成功すると Loading development environment (Rails 4.2.8) irb(main):001:0> みたいな画面が表示されます。 そこで Card.all ですCは大文字です。
webillion

2017/04/05 06:01

ありがとうございます。下記、結果になります。 C:\Users\daino\Desktop\RailsApp>rails c Loading development environment (Rails 5.0.2) irb(main):001:0> Card.all Card Load (15.6ms) SELECT "cards".* FROM "cards" => #<ActiveRecord::Relation []> irb(main):002:0>
moke

2017/04/05 06:14

結論、cardが一つも登録されていない。 rails c が開いたまま Card.create(title: 'タイトル', author: '担当', price: 100, publisher: '俺', memo: '特になし') を打ち込みましょうエラーが出たら author: '担当', price: 100, publisher: '俺',の部分を削除して再チャレンジ 最後にもう一度Card.allをしてください
webillion

2017/04/05 06:30

Card.create(title: 'タイトル', author: '担当', price: 100, publisher: '俺', memo: '特になし') 上記をコマンドプロンプトで入力すると、'タイトル'や'特になし'の日本語が別の文字に変換されて正しく入力ができないのですが、別の文字とかで代用したほうが良いでしょうか?
moke

2017/04/05 06:39 編集

好きなのでいいですよ。ブラウザとOSとterateilの仕様とCUIが複合的に絡んで文字化けはおこりますからねー いっそのこと半角英数にしてみれば Card.create(title: 'title', author: 'user', price: 100, publisher: 'me', memo: 'nothing') コピペでオッケーです。
webillion

2017/04/05 06:42

下記、コピペした結果になります。これはエラーでしょうか? C:\Users\daino\Desktop\RailsApp>rails c Loading development environment (Rails 5.0.2) irb(main):001:0> Card.create(title: 'title', author: 'user', price: 100, publisher: 'me', memo: 'nothing') (0.0ms) begin transaction SQL (15.6ms) INSERT INTO "cards" ("title", "author", "price", "publisher", "memo", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?) [["title", "title"], ["author", "user"], ["price", 100], ["publisher", "me"], ["memo", "nothing"], ["created_at", 2017-04-05 06:40:12 UTC], ["updated_at", 2017-04-05 06:40:12 UTC]] (31.3ms) commit transaction => #<Card id: 1, title: "title", author: "user", price: 100, publisher: "me", memo: "nothing", created_at: "2017-04-05 06:40:12", updated_at: "2017-04-05 06:40:12"> irb(main):002:0>
moke

2017/04/05 06:51 編集

エラーじゃないと思いますが Card.allの結果を一緒に書いてくれれば、返信が一回で済むのですが…。 まあいいやquitでrails cを終了して再度 rails s そして localhost:3000/cards/1 にアクセスしてみてください
webillion

2017/04/05 06:58

再度、「localhost:3000/cards/1」にアクセスしたところ、ブラウザに下記のエラーが出力されました。 Template is missing Missing template layouts/cards with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder]}. Searched in: * "C:/Users/daino/Desktop/RailsApp/app/views" Extracted source (around line #46): 44 45 46 47 48 49 def find(*args) find_all(*args).first || raise(MissingTemplate.new(self, *args)) end def find_file(path, prefixes = [], *args)
moke

2017/04/05 07:21

うーんと、その教本ですがちゃんと順番通りにやっていますか? エラーが出たのを無視して必要な処理を飛ばしているのではないでしょうか? その本自体は読んだことがないのでなんとも言えませんが、 必ず、できているかどうかのチェックがあると思います。 必ずチェックポイントを一つ一つ積み重ねてください。 ちなみにこの場合はviewsのshow.html.erbがない状態です。
webillion

2017/04/05 07:47

途中まではエラーを出さずに順調に進めていたのですが、質問で投稿した箇所を追加したところ、エラーが発生しました。見直してみます。
webillion

2017/04/05 08:09

よく見直したところ、踏まえるべき手順を飛ばしていたことが分かりました。 ご迷惑をおかけしました。 ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問