@contek
Revisions
Report this question
Subscribe question
Help us understand the problem. What is going on with this question?
Q&A

Ajax Rails反映されなくて困っています

非同期通信を使うコメント投稿機能を実装出来なくて困っております。
(現状はリロードしなければコメントが反映されません💦)

エラー内容

messages_controllers.rb
 app/controllers/messages_controller.rb:7:in `create'
No template found for MessagesController#create, rendering head :no_content
Completed 204 No Content in 139ms (ActiveRecord: 7.3ms | Allocations: 6668)
messages_controllers.rb
class MessagesController < ApplicationController

  def create
    @message = Message.new(message_params.merge(user_id: current_user.id))
    if @message.save
    else
      respond_to do |format|
        format.html { redirect_to conversation_path(@conversation.id) }
        format.js
        redirect_back(fallback_location: root_path)
      end
    end
  end

    protected

     def message_params
       params.require(:message).permit(:content, :conversation_id, :user_id)
     end
end
conversations_controllers.rb
class ConversationsController < ApplicationController

  def show
    if @conversation = Conversation.find(params[:id])
      @messages = @conversation.messages
      @admin = User.admin
      @message = Message.new
    else
      redirect_back(fallback_location: root_path)
    end
  end
end
conversations_show.html.erb
<div id='message_area'>
  <%= render "messages", conversation: @conversation, messages: @messages %>
</div>

<div id='message_form'>
  <%= render "form", conversation: @conversation, message: @message %>
</div>
_messages.html.erb
<% if messages.present? %>

  <% messages.each do |msg| %>
    <%= msg.content %>
  <% end %>

<% else %>
  <h3 class="text-center">まだ会話は行われていません</h3>
<% end %> 
_form.html.erb
<%= form_with(model: message, remote: true) do |f| %>
  <%= f.text_field :content %>
  <%= f.hidden_field :conversation_id, value: conversation.id %>
  <%= f.submit "送信" %>
<% end %>
create.js.erb
$("#message_area").html("<%= escape_javascript(render(partial: "conversations/messages", locals: { conversation: @conversation, messages: @messages })) %>");
$("#message_form").html("<%= escape_javascript(render(partial: "conversations/form", locals: { conversation: @conversation, message: @message })) %>");

controllerに何か記述する必要があるのでしょうか💦
すみませんが、ご教授の程よろしくお願いします。

0
1
Answer

これが近いのかなと思ったのですが、いかがでしょうか?
https://teratail.com/questions/188561

0
@sayama0402さん、
ご回答いただき、ありがとうございます。

リンク先を参考の結果、かなり修正できました!
しかし、やはりエラーが出力されリロードが必要になってしまします。

改めて編集し直したファイルを載せておりますが、何が問題なのでしょうか💦
Help us understand the problem. What is going on with this answer?
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした