エラーメッセージ内容
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
です
回答1件
あなたの回答
tips
プレビュー