Basically, I have my messages inbox (using mailboxer gem) arranged in a collapsible accordion (using twitter bootstrap). Whenever a user clicks on a conversation, it expands and shows all the messages. I want it so that when the user clicks on the conversation, it also marks the conversation as read.
I followed the instructions here to try to do what I want it to do: How to call a rails method from in jQuery
I basically tried to reference the "a data-toggle=......"by putting an id in there and then running a jquery script that gets a render_read path I created in my controller whenever I click something in between the "a data-toggle=....." and "/a" tags (stakoverflow won't let me carrot the a's..)
It's not working though....please help, I've been trying to solve this problem for the past day....I'm also relatively new at rails.
_conversation.html.erb (Mailboxer uses this to render each conversation when I run 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>
I run this script at the bottom of this file
<script>$("#click_<%= conversation.id %>").click(function() {$.ajax("/conversations/render_read")});</script>
My conversations controller has in public section
def render_read
conversation.mark_as_read(current_user)
end
My routes.rb has
resources :conversations, only: [:index, :show, :new, :create] do
member do
post :reply
post :trash
post :untrash
get :render_read
end
end