コメント機能の実装を行っている最中ですが,コメントを送信しようとすると保存されずルーティングエラーになりました。
prototypescontroller class PrototypesController < ApplicationController #CSRF保護を無効にする protect_from_forgery with: :null_session before_action :move_to_index, except: [:index, :show] def index @prototype = Prototype.all end def new @prototype = Prototype.new end def create @prototype = Prototype.new(prototype_params) if @prototype.save redirect_to root_path(@prototype) else render :new @prototype = Prototype.includes(:user) end end def show @prototype = Prototype.find(params[:id]) @comment = @prototype.comment.new(comment_params) @comments = @prototype.comments.includes(:user) end def edit @prototype = Prototype.find(params[:id]) end def update @prototype = Prototype.find(params[:id]) @prototype.update(prototype_params) if @prototype.save redirect_to root_path(@prototype) else render :edit end end def destroy prototype = Prototype.find(params[:id]) prototype.destroy redirect_to root_path end private def prototype_params params[:prototype].permit(:title, :catch_copy, :concept, :image).merge(user_id: current_user.id) end def move_to_index unless user_signed_in? redirect_to action: :index end end def move_to_update unless user_signed_in? redirect_to action: :index end end def move_to_destroy unless user_signed_in? redirect_to action: :index end end end
comments_controller.rb class CommentsController < ApplicationController def create @comment = Comment.new(comment_params) if @comment.save redirect_to prototype_path(@comment.prototype) else @prototype = @comment.prototype @comment = @prototype.comment render :"prototypes/show" end end private def comment_params params[:comment].permit(:text, :prototype, :user).merge(user_id: current_user.id, prototype_id: params[:prototype_id]) end end
routes.rb Rails.application.routes.draw do devise_for :users root to: "prototypes#index" resources :prototypes, only: [:index, :new, :create, :show, :edit, :update, :destroy] resources :comments, only: [:create, :show] end
試したこと
routes.rbの記述に全角スペースがないか確認した。
回答1件
あなたの回答
tips
プレビュー