Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

コメントが保存されません。URLがおかしい?

解決したいこと

chatroomを作っており、作成したルームの中にユーザーを招待でき、メッセージの投稿ができます。
そのメッセージをクリックすると、コメント投稿ページに遷移するのですが、そのコメント投稿ページの
URLが下記のようになっており、明らかにおかしい状況です。それによって、コメントが保存できないエラー等が
引き起こされているように思います。何が問題で、どのようにしたら正しいURLが表示され、コメントが保存できるように
なるのでしょうか。

動作確認のGif動画です。
https://gyazo.com/084d4dde9b3a72c7ae96cd6a95cfc242

該当するソースコード

class CommentsController < ApplicationController

  def index    
    @messages = Message.all
    @room = Room.all

  end

  def new
    @comments = Comment.new
    @messages = Message.all
    @room = Room.all
  end

  def create
     if @comment = Comment.create(comment_params)
        redirect_to root_path

     end
  end

      private
      def comment_params
        params.require(:comment).permit(:text).merge(user_id: current_user.id, message_id: params[:message_id], room_id: params[:room_id])
      end
end

class MessagesController < ApplicationController
  def index
    @comment = Comment.new
    @message = Message.new
    @room = Room.find(params[:room_id])
    @messages = @room.messages.includes(:user).order("created_at DESC")
    @users = @room.users.order("student_number ASC")
  end

  def new
    @comment = Comment.new
  end

  def show
    @comment = Comment.new
    @comments = @message.comments.includes(:user)
  end

  def create
    @room = Room.find(params[:room_id])
    @message = @room.messages.new(message_params)
    if @message.save
      redirect_to room_messages_path(@room)
    else
      @messages = @room.messages.includes(:user)
      render :index
    end
  end


  private

  def message_params
    params.require(:message).permit(:content, :image).merge(user_id: current_user.id)
  end
end

このビューが表す実際の画像です。
https://gyazo.com/8d9354c3f5b0269f8becc2634c64064b

rails routes実行後の遷移先パス確認用の画像
https://gyazo.com/65640d184d63a704db80b5255520fc6a

<div class="chat-header">
  <div class="left-header">
    <div class="header-title">
      <%= @room.name %>
    </div>
  </div>
  <div class="right-header">
    <div class="header-button">
    <% if current_user.genre == "教員"%>
      <%=link_to "ルーム削除", room_path(@room), method: :delete %>
    <% end %>
    </div>
  </div>
</div>

<div class="messages">
   <div class="message">
  <div class="upper-message">
    <div class="message-user">
      <%= message.user.name %>
    </div>
    <div class="message-date">
      <%=  l message.created_at %>
    </div>
  </div>
  <div class="lower-message">
    <div class="message-content">
      <%=link_to message.content, new_message_comment_path(@room, @messages) %>
    </div>
     <%= image_tag message.image.variant(resize: '400x400'), class: 'message-image' if message.image.attached? %>
  </div>

</div>
</div>

<%= form_with model: @message, class: 'form', local: true do |f| %>
  <div class="form-input">
    <%= f.text_field :content, class: 'form-message', placeholder: 'type a message' %>
    <label class="form-image">
      <span class="image-file">画像</span>
      <%= f.file_field :image, class: 'hidden' %>
    </label>
  </div>
  <%= f.submit '送信', class: 'form-submit' %>
<% end %>
Rails.application.routes.draw do
  devise_for :users
  get 'messages/index'
  get 'comments/index'
  post 'comments/new'
  root to: "rooms#index"
  resources :users
  resources :rooms do
    resources :messages
  end
  resources :messages do
    resources :comments
end
end

Parameters:

{"authenticity_token"=>"hOl+SYqAwHyds15WCM8y6/Nk8INXYFYxwK5GHjOeRRPJkyCI65t/+Rj6ZnSLLANqOap4ODaG8hclLOHcK3l1+Q==",
 "message"=>{"text"=>"あああ"},
 "commit"=>"コメントする",
 "message_id"=>"#<Message::ActiveRecord_Relation:0x00007ff9c296dac0>"}
0 likes

2Answer

とりあえずぱっと見で分かるのは

"message_id"=>"#<Message::ActiveRecord_Relation:0x00007ff9c296dac0>"

の部分ですね。IDにオブジェクトが入っています。

form_withで実際に出力されるHTML自体を読んでみるとなにかヒントがあるかもしれません。

1Like

Comments

  1. @Moto-kawakatsu

    Questioner

    ありがとうございます!何とか解決できました!

@Moto-kawakatsu

回答ではないですが、質問の書き方について一点補足を。。

GyazoのGIF動画は直接埋め込むこともできますよ。
↓こんな感じです。
Image from Gyazo

Gyazoの「シェア > マークダウンをコピー」でコピーしたものを貼り付けるだけで出来ます。
image.png

0Like

Your answer might help someone💌