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

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

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

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

Q&A

解決済

1回答

2807閲覧

いいね!ボタンを押すと謎のエラーメッセージが出現します

ninpig04

総合スコア33

Ruby on Rails

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

0グッド

0クリップ

投稿2018/02/12 22:57

編集2018/02/13 12:22

エラーメッセージ内容

PostsController#show is missing a template for this request format and variant. request.formats: ["text/html"] request.variant: [] NOTE! For XHR/Ajax or API requests, this action would normally respond with 204 No Content: an empty white screen. Since you're loading it in a web browser, we assume that you expected to actually render a template, not nothing, so we're showing an error to be extra-clear. If you expect 204 No Content, carry on. That's what you'll get from an XHR or API request. Give it a shot.

"That's what you'll get from an XHR or API request. Give it a shot." raise ActionController::UnknownFormat, message else logger.info "No template found for #{self.class.name}\##{action_name}, rendering head :no_content" if logger super

いいね!ボタンを押すとこのようなエラーメッセージが出ます。
ページを戻してリロードするといいね!ボタンが押したことはセーブされているので、あとは元のページに戻ってくれれば・・・と思うのですが、何が問題なのでしょうか?
自分では解決できませんでした。。。
よろしくお願いします。

以下の情報を追記してもらえますか。①config/routes.rb,②「いいね」を押したときのリクエスト(ログファイルもしくは rails server したターミナルから拾う),③「いいね」を押したときのアクションのメソッド,④posts#show アクションのメソッド,⑤posts#show アクションに対応するビューのテンプレートのパス

Rails.application.routes.draw do post "likes/:post_id/create" => "likes#create" post "likes/:post_id/destroy" => "likes#destroy" get "posts/index" => "posts#index" get "posts/new" => "posts#new" get "posts/:id" => "posts#show" post "posts/create" => "posts#create" get 'comments/index' get 'comments/:id' => 'comments#show' root 'static_pages#home' get '/help', to: 'static_pages#help' get '/configuration', to: 'static_pages#configuration' get '/about', to: 'static_pages#about' get '/contact', to: 'static_pages#contact' get '/top', to: 'static_pages#top' get '/signup', to: 'users#new' get '/login', to: 'sessions#new' post '/login', to: 'sessions#create' delete '/logout', to: 'sessions#destroy' get '/:city/:banti', to: 'comments#index' resources :users do member do get :following, :followers end end resources :account_activations, only: [:edit] resources :password_resets, only: [:new, :create, :edit, :update] resources :microposts, only: [:create, :destroy] resources :relationships, only: [:create, :destroy] end

いいねを押した時のアクション

class LikesController < ApplicationController def index end def create @like = Like.new(user_id: @current_user.id, post_id: params[:post_id]) @like.save redirect_to("/posts/#{params[:post_id]}") end def destroy @like = Like.find_by(user_id: @current_user.id, post_id: params[:post_id]) @like.destroy redirect_to("/posts/#{params[:post_id]}") end end

posts#showのメソッド(いいねのページはCommentsのshowです)

def show @post = Post.find_by(id: params[:id]) @likes_count = Like.where(post_id: @post.id).count end

パスは
app/views/posts/show.html.erbです

ちなみに、comments#showのメソッドは

class CommentsController < ApplicationController def show @comments = Comment.find_by(id: params[:id]) @posts = Post.where(comment_id: @comment.id) @current_user = User.find_by(id: session[:user_id]) @post = Post.find_by(id: params[:id]) @likes_count = Like.where(post_id: @post.id).count end end

です

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

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

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

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

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

scivola

2018/02/13 06:15

以下の情報を追記してもらえますか。①config/routes.rb,②「いいね」を押したときのリクエスト(ログファイルもしくは rails server したターミナルから拾う),③「いいね」を押したときのアクションのメソッド,④posts#show アクションのメソッド,⑤posts#show アクションに対応するビューのテンプレートのパス
ninpig04

2018/02/13 12:22

log ```ここに言語を入力 Started GET "/posts/4" for ::1 at 2018-02-13 21:12:46 +0900 Processing by PostsController#show as HTML Parameters: {"id"=>"4"} User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]] Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT ? [["id", 4], ["LIMIT", 1]]  (0.2ms) SELECT COUNT(*) FROM "likes" WHERE "likes"."post_id" = ? [["post_id", 4]] Completed 406 Not Acceptable in 114ms (ActiveRecord: 0.4ms) ActionController::UnknownFormat (PostsController#show is missing a template for this request format and variant. request.formats: ["text/html"] request.variant: [] NOTE! For XHR/Ajax or API requests, this action would normally respond with 204 No Content: an empty white screen. Since you're loading it in a web browser, we assume that you expected to actually render a template, not nothing, so we're showing an error to be extra-clear. If you expect 204 No Content, carry on. That's what you'll get from an XHR or API request. Give it a shot.): vendor/bundle/gems/actionpack-5.1.4/lib/action_controller/metal/implicit_render.rb:53:in `default_render' vendor/bundle/gems/actionpack-5.1.4/lib/action_controller/metal/basic_implicit_render.rb:4:in `block in send_action' vendor/bundle/gems/actionpack-5.1.4/lib/action_controller/metal/basic_implicit_render.rb:4:in `tap' vendor/bundle/gems/actionpack-5.1.4/lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action' vendor/bundle/gems/actionpack-5.1.4/lib/abstract_controller/base.rb:186:in `process_action' vendor/bundle/gems/actionpack-5.1.4/lib/action_controller/metal/rendering.rb:30:in `process_action' vendor/bundle/gems/actionpack-5.1.4/lib/abstract_controller/callbacks.rb:20:in `block in process_action' vendor/bundle/gems/activesupport-5.1.4/lib/active_support/callbacks.rb:131:in `run_callbacks' vendor/bundle/gems/actionpack-5.1.4/lib/abstract_controller/callbacks.rb:19:in `process_action' vendor/bundle/gems/actionpack-5.1.4/lib/action_controller/metal/rescue.rb:20:in `process_action' vendor/bundle/gems/actionpack-5.1.4/lib/action_controller/metal/instrumentation.rb:32:in `block in process_action' vendor/bundle/gems/activesupport-5.1.4/lib/active_support/notifications.rb:166:in `block in instrument' vendor/bundle/gems/activesupport-5.1.4/lib/active_support/notifications/instrumenter.rb:21:in `instrument' vendor/bundle/gems/activesupport-5.1.4/lib/active_support/notifications.rb:166:in `instrument' vendor/bundle/gems/actionpack-5.1.4/lib/action_controller/metal/instrumentation.rb:30:in `process_action' vendor/bundle/gems/actionpack-5.1.4/lib/action_controller/metal/params_wrapper.rb:252:in `process_action' vendor/bundle/gems/activerecord-5.1.4/lib/active_record/railties/controller_runtime.rb:22:in `process_action' vendor/bundle/gems/actionpack-5.1.4/lib/abstract_controller/base.rb:124:in `process' vendor/bundle/gems/actionview-5.1.4/lib/action_view/rendering.rb:30:in `process' vendor/bundle/gems/actionpack-5.1.4/lib/action_controller/metal.rb:189:in `dispatch' vendor/bundle/gems/actionpack-5.1.4/lib/action_controller/metal.rb:253:in `dispatch' vendor/bundle/gems/actionpack-5.1.4/lib/action_dispatch/routing/route_set.rb:49:in `dispatch' vendor/bundle/gems/actionpack-5.1.4/lib/action_dispatch/routing/route_set.rb:31:in `serve' vendor/bundle/gems/actionpack-5.1.4/lib/action_dispatch/journey/router.rb:50:in `block in serve' vendor/bundle/gems/actionpack-5.1.4/lib/action_dispatch/journey/router.rb:33:in `each' vendor/bundle/gems/actionpack-5.1.4/lib/action_dispatch/journey/router.rb:33:in `serve' vendor/bundle/gems/actionpack-5.1.4/lib/action_dispatch/routing/route_set.rb:834:in `call' vendor/bundle/gems/rack-2.0.3/lib/rack/etag.rb:25:in `call' vendor/bundle/gems/rack-2.0.3/lib/rack/conditional_get.rb:25:in `call' vendor/bundle/gems/rack-2.0.3/lib/rack/head.rb:12:in `call' vendor/bundle/gems/rack-2.0.3/lib/rack/session/abstract/id.rb:232:in `context' vendor/bundle/gems/rack-2.0.3/lib/rack/session/abstract/id.rb:226:in `call' vendor/bundle/gems/actionpack-5.1.4/lib/action_dispatch/middleware/cookies.rb:613:in `call' vendor/bundle/gems/activerecord-5.1.4/lib/active_record/migration.rb:556:in `call' vendor/bundle/gems/actionpack-5.1.4/lib/action_dispatch/middleware/callbacks.rb:26:in `block in call' vendor/bundle/gems/activesupport-5.1.4/lib/active_support/callbacks.rb:97:in `run_callbacks' vendor/bundle/gems/actionpack-5.1.4/lib/action_dispatch/middleware/callbacks.rb:24:in `call' vendor/bundle/gems/actionpack-5.1.4/lib/action_dispatch/middleware/executor.rb:12:in `call' vendor/bundle/gems/actionpack-5.1.4/lib/action_dispatch/middleware/debug_exceptions.rb:59:in `call' vendor/bundle/gems/web-console-3.5.1/lib/web_console/middleware.rb:135:in `call_app' vendor/bundle/gems/web-console-3.5.1/lib/web_console/middleware.rb:28:in `block in call' vendor/bundle/gems/web-console-3.5.1/lib/web_console/middleware.rb:18:in `catch' vendor/bundle/gems/web-console-3.5.1/lib/web_console/middleware.rb:18:in `call' vendor/bundle/gems/actionpack-5.1.4/lib/action_dispatch/middleware/show_exceptions.rb:31:in `call' vendor/bundle/gems/railties-5.1.4/lib/rails/rack/logger.rb:36:in `call_app' vendor/bundle/gems/railties-5.1.4/lib/rails/rack/logger.rb:24:in `block in call' vendor/bundle/gems/activesupport-5.1.4/lib/active_support/tagged_logging.rb:69:in `block in tagged' vendor/bundle/gems/activesupport-5.1.4/lib/active_support/tagged_logging.rb:26:in `tagged' vendor/bundle/gems/activesupport-5.1.4/lib/active_support/tagged_logging.rb:69:in `tagged' vendor/bundle/gems/railties-5.1.4/lib/rails/rack/logger.rb:24:in `call' vendor/bundle/gems/actionpack-5.1.4/lib/action_dispatch/middleware/remote_ip.rb:79:in `call' vendor/bundle/gems/actionpack-5.1.4/lib/action_dispatch/middleware/request_id.rb:25:in `call' vendor/bundle/gems/rack-2.0.3/lib/rack/method_override.rb:22:in `call' vendor/bundle/gems/rack-2.0.3/lib/rack/runtime.rb:22:in `call' vendor/bundle/gems/activesupport-5.1.4/lib/active_support/cache/strategy/local_cache_middleware.rb:27:in `call' vendor/bundle/gems/actionpack-5.1.4/lib/action_dispatch/middleware/executor.rb:12:in `call' vendor/bundle/gems/actionpack-5.1.4/lib/action_dispatch/middleware/static.rb:125:in `call' vendor/bundle/gems/rack-2.0.3/lib/rack/sendfile.rb:111:in `call' vendor/bundle/gems/railties-5.1.4/lib/rails/engine.rb:522:in `call' vendor/bundle/gems/puma-3.9.1/lib/puma/configuration.rb:224:in `call' vendor/bundle/gems/puma-3.9.1/lib/puma/server.rb:602:in `handle_request' vendor/bundle/gems/puma-3.9.1/lib/puma/server.rb:435:in `process_client' vendor/bundle/gems/puma-3.9.1/lib/puma/server.rb:299:in `block in run' vendor/bundle/gems/puma-3.9.1/lib/puma/thread_pool.rb:120:in `block in spawn_thread Started POST "/likes/6/create" for ::1 at 2018-02-13 21:12:58 +0900 Processing by LikesController#create as HTML ```
ninpig04

2018/02/13 12:23

こちらはlogになります。他の情報は本文に追加しました。よろしくお願いいたします。
ninpig04

2018/02/13 15:56

postsのshowのビューを作ったらうまく行きました! しかし、いいね!ボタンを押して元のcommentsのshowに戻るようにしたいのですが、そうするとリダイレクトエラーと表示されてエラーが出てしまいます。これはなぜでしょうか?
scivola

2018/02/13 16:04

えっ?! ビュー作ってなかったの??? 「パスは app/views/posts/show.html.erbです」とあったので二時間くらい悩んでたんですが。
scivola

2018/02/13 16:07

「いいね!ボタンを押して元のcommentsのshowに戻るようにしたいのですが、そうするとリダイレクトエラーと表示されてエラーが出てしまいます。」をもっと具体的に書いてください。
scivola

2018/02/13 16:09

「いいね」はコメントに対する「いいね」じゃなくて,Post に対する「いいね」なんですよね? それで,「いいね」したあと comments#show を表示する,というのがよくわからないですが。
ninpig04

2018/02/13 17:00

ごめんなし、しっかり確認できていませんでした。。。。 likes_controllerの中にあるcreateアクションとdestroyアクションは実行した後/posts/#{params[:post_id]}にリダイレクトされるように記述しましたが、comments/:idにリダイレクトしたいのです。しかしこれを実行すると、このページは動作していません localhost から無効な応答が送信されました。 ERR_INVALID_REDIRECTと表示されてしまいます。
ninpig04

2018/02/13 17:02

postのフォームはcomments/showのページに存在しています。また、そこにPostモデルで取り出した投稿データを一覧にして表示しています。そこLike_controllerを使っていいねをつけたり消したりしたりできるようにしているのです。
guest

回答1

0

ベストアンサー

ruby詳しいわけではないですが、エラーメッセージ読んだら割と簡単なことのように思います。

PostsController#showには、このリクエストフォーマットとバリアントのテンプレートがありません。 request.formats:["text / html"] request.variant:[]注意! XHR / AjaxまたはAPIリクエストの場合、このアクションは通常、No Content:空の白い画面で応答します。 Webブラウザで読み込んでいるので、実際にテンプレートをレンダリングすることを想定していると想定していますので、余分なエラーを表示しています。 あなたが204のコンテンツを期待するなら、続けてください。 それがXHRまたはAPIリクエストから得られるものです。 試してみます。

*Google翻訳

非同期通信でコントローラ利用してhtmlとして出力する必要がなくてもきちんと空白でもビューを用意しろってことですかね。たぶん。

投稿2018/02/13 00:15

編集2018/02/13 00:16
m.ts10806

総合スコア80842

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

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

ninpig04

2018/02/13 15:56

postsのshowのビューを作ったらうまく行きました! しかし、いいね!ボタンを押して元のcommentsのshowに戻るようにしたいのですが、そうするとリダイレクトエラーと表示されてエラーが出てしまいます。これはなぜでしょうか?
m.ts10806

2018/02/13 21:22

非同期通信のビューはあくまで通信処理結果のみ(例えば成功・失敗のみ)であってリダイレクト先htmlであってはいけません。 例えば「1」とだけ出力するビューを用意するだけではないかと思いますよ。 そのビューは元々の画面と同じであってはいけないように思います(いいねボタンで同期POSTする場合はこの限りではありません) html部分、javascript部分などビュー側のソースも提示された方がより的確な回答を得られるかと思います。 (あと、「ruby 非同期通信」とか「ruby ajax」とかで調べるとか)
scivola

2018/02/13 23:22

Ajax 使ってます? エラーメッセージ中に Ajax に関する記述がありますが,ninpig04 さんのコードでは Ajax は使ってませんよね?
m.ts10806

2018/02/14 00:25

scivolaさん あ、確かにそうですね。エラー文の「XHR / Ajax」というところから非同期→ajaxを利用していると解釈していました。失礼しました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.49%

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

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

質問する

関連した質問