基本的に、メッセージの受信トレイ (mailboxer gem を使用) を折りたたみ可能なアコーディオン (twitter ブートストラップを使用) に配置しています。ユーザーが会話をクリックすると、会話が展開され、すべてのメッセージが表示されます。ユーザーが会話をクリックすると、会話が既読としてマークされるようにしたい。
ここの指示に従って、やりたいことをやろうとしました: How to call a rails method from in in jQuery
私は基本的に「a data-toggle=......」を参照しようとしました。そこに id を入れてから、「a data-toggle=....." および "/a" タグ (stakoverflow では a をキャロットさせてくれません..)
それはうまくいきません....助けてください、私はこの問題を過去1日間解決しようとしています....私はレールにも比較的慣れていません。
_conversation.html.erb (mailboxer はこれを使用して、mailbox.inbox を実行したときに各会話をレンダリングします)
<div class="panel panel-default" >
<div class="panel-heading">
<h3 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href= "#collapse_<%= conversation.id %>" id= "click_<%= conversation.id %>">
<strong><%= conversation.subject %> </strong>
</a>
</h3>
</div>
<div id="collapse_<%= conversation.id %>" class="panel-collapse collapsing">
<div class="panel-body">
<%= content_tag_for(:ul, conversation.receipts_for(current_user)) do |receipt| %>
<% message = receipt.message %>
<% t = message.updated_at.localtime %>
<div class="row">
<div class="col-md-7"><strong><%= message.sender.first_name %>: </strong><%= message.body %></div>
<div class="col-md-5">Sent <%= t.strftime("%a, %m/%e/%Y %I:%M %p") %></div>
</div>
<% end %>
<%= render 'messages/form', conversation: conversation %>
</div>
</div>
</div>
このファイルの最後でこのスクリプトを実行します
<script>$("#click_<%= conversation.id %>").click(function() {$.ajax("/conversations/render_read")});</script>
私の会話コントローラーは公開セクションにあります
def render_read
conversation.mark_as_read(current_user)
end
私のroutes.rbには
resources :conversations, only: [:index, :show, :new, :create] do
member do
post :reply
post :trash
post :untrash
get :render_read
end
end