###前提・実現したいこと
rails gem 'ancestry'を使ってcommentに対してreplyを行いたいと思い、
以下の設定で行っていたのですが、ActionController::UrlGenerationError in Messages#showでつまづいてしまいました。どのようにしたらよろしいでしょうか?
###発生している問題・エラーメッセージ
エラーメッセージ ActionController::UrlGenerationError in Messages#show No route matches {:action=>"new", :controller=>"comments", :id=>"1", :parent_id=>#<Comment id: 1, content: "test", message_id: 1, user_id: 1, created_at: "2017-04-23 04:20:30", updated_at: "2017-04-23 04:20:30", ancestry: nil>} missing required keys: [:message_id] p>Posted: <%= link_to comment.user.username, user_path(user) %><%= image_tag (user.avatar.url(:thumb)) unless user.avatar.blank? %> <%= comment.created_at.strftime("%Y/%m/%d %H:%M") %> <%= link_to "Reply", new_message_comment_path(:parent_id => comment) %></p> Request Parameters: {"id"=>"1"}
###該当のソースコード
class AddAncestryToComments < ActiveRecord::Migration[5.0] def change add_column :comments, :ancestry, :string add_index :comments, :ancestry end def self.down remove_column :comments, :ancestry remove_index :comments, :ancestry end end
comment.rb
class Comment < ApplicationRecord belongs_to :message belongs_to :user has_ancestry end
messages/show
<%= link_to "Reply", new_message_comment_path(:parent_id => comment) %>
comments_controller
class CommentsController < ApplicationController before_action :find_message, only: [:create, :edit, :update, :destroy] before_action :find_comment, only: [:edit, :update, :destroy] before_action :authenticate_user! def new @comment = Comment.new(:parent_id => params[:parent_id]) end def create @comment = @message.comments.create(comment_params) @comment.user_id = current_user.id if @comment.save redirect_to message_path(@message) else render 'new' end end def edit end def update if @comment.update(comment_params) redirect_to message_path(@message) else render 'edit' end end def destroy @comment.destroy redirect_to message_path(@message) end private def comment_params params.require(:comment).permit(:content, :parent_id) end def find_message @message = Message.find(params[:message_id]) end def find_comment @comment = @message.comments.find(params[:id]) end end
comment routes
message_comments POST /messages/:message_id/comments(.:format) comments#create new_message_comment GET /messages/:message_id/comments/new(.:format) comments#new edit_message_comment GET /messages/:message_id/comments/:id/edit(.:format) comments#edit message_comment PATCH /messages/:message_id/comments/:id(.:format) comments#update PUT /messages/:message_id/comments/:id(.:format) comments#update DELETE /messages/:message_id/comments/:id(.:format) comments#destroy
routes.rb
resources :messages do member do post "add", to: "favorites#create" get "show_favorites" => "favorites#show_favorites" end resources :comments, only: [:create, :edit, :update, :destroy, :new] end
###試したこと
UrlGenerationErrorとなってたので、routesでget '/comments/new/(:parent_id)', to: 'comments#new'を設定してみたのですがダメでした。
###補足情報(言語/FW/ツール等のバージョンなど)
より詳細な情報
ruby on rails, cloud9
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。