前提・実現したいこと
Railsにて、Q&Aとブログ機能を両方持たせたようなサイトを作っております。
そこで、questionsコントローラー及び、postsコントローラー両方に
ログインユーザーのみdestroy/edit/updateできる機能を実装しようとしていますが
エラーが出てきてしまいます。
困ってしまっており、どなたかお助けいただけませんでしょうか。
発生している問題・エラーメッセージ
Unknown action The action 'update' could not be found for QuestionsController
NoMethodError in Posts#edit Showing /Users/meo/projects/torehack/app/views/posts/edit.html.erb where line #5 raised: undefined method `id' for nil:NilClass Extracted source (around line #5): <h1 class="form-heading">編集する</h1> <%= form_tag("/posts/#{@post.id}/update") do %> #エラーはここです! <div class="form"> <div class="form-body">
Unknown action The action 'destroy' could not be found for PostsController
該当のソースコード
postsコントローラー def edit @post = Post.find(params[:id]) end def update @post = Post.find_by(id:params[:id]) @post.title =params[:title] @post.content =params[:content] if @post.save flash[:notice] = "投稿を編集しました" redirect_to("/posts/index") else render("posts/edit") end end def destroy @post = Post.find_by(id: params[:id]) @post.destroy flash[:notice] = "投稿を削除しました" redirect_to("/posts/index") end def ensure_correct_user @post = Post.find_by(id: params[:id]) if @post.user_id != @current_user.id flash[:notice] = "権限がありません" redirect_to("/posts/index") end end end questionsコントローラー def edit @question = Question.find_by(id: params[:id]) end def update @question = Question.find_by(id:params[:id]) @question.title =params[:title] @question.content =params[:content] if @question.save flash[:notice] = "投稿を編集しました" redirect_to("/questions/index") else render("questions/edit") end end def destroy @question = Question.find_by(id: params[:id]) @question.destroy flash[:notice] = "投稿を削除しました" redirect_to("/questions/index") end end
試したこと
試行錯誤しておりますが、経験も浅く対処できそうにありません・・・・
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
あなたの回答
tips
プレビュー