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

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

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

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

Q&A

解決済

1回答

4298閲覧

UrlgenerationErrorを直したい

satail

総合スコア31

Ruby on Rails

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

0グッド

0クリップ

投稿2017/05/19 17:45

編集2017/05/19 17:46

###前提・実現したいこと
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

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

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

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

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

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

guest

回答1

0

自己解決

<%= link_to "Reply", new_message_comment_path(comment.message, :parent_id => comment.id) %>

とすることによって、エラーをなくすことができました。

投稿2017/05/21 13:25

satail

総合スコア31

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問