Rails create action Ajax returns Template Missing or Unknown Fomat -
i'm trying implement private_pub on one-to-one chat app, without private_pub works fine not ideal chat app since no websocket used autoupdates. tried implementing on railscasts of private_pub getting either template missing or unknown format. here code:
routes
resources :conversations resources :messages end
i have button in user show page start chat(which creates/uses conversation , shows conversation show page 1 one chat box on facebook/gchat , conversation has many messages):
<% unless current_user == @user %> <%= link_to "send message", conversations_path(:sender_id => current_user.id , :recipient_id => @user.id ), :method => :post, class: "btn btn-success btn-xs start-conversation" %> <% end %>
conversation controller
class conversationscontroller < applicationcontroller before_filter :authenticate_user! layout false def create if conversation.between(params[:sender_id],params[:recipient_id]).present? @conversation = conversation.between(params[:sender_id],params[:recipient_id]).first else @conversation = conversation.create!(conversation_params) end @conversation.save! redirect_to @conversation end def show @conversation = conversation.find(params[:id]) @reciever = interlocutor(@conversation) @messages = @conversation.messages @message = message.new end private def conversation_params params.permit(:sender_id, :recipient_id) end def interlocutor(conversation) current_user == conversation.recipient ? conversation.sender : conversation.recipient end end
convesation show view
<% content_for :bottom %> <%= subscribe_to conversation_path(@conversation) %> <% end %> <div class="chatboxhead"> <div class="chatboxtitle"> <i class="fa fa-comments"></i> <h1><%= @reciever.username %> </h1> </div> <div class="chatboxoptions"> <%= link_to "<i class='fa fa-minus'></i> ".html_safe, "#", class: "togglechatbox", "data-cid" => @conversation.id %> <%= link_to "<i class='fa fa-times'></i> ".html_safe, "#", class: "closechat", "data-cid" => @conversation.id %> </div> <br clear="all"/> </div> <div class="chatboxcontent"> <% if @messages.any? %> <%= render @messages.reverse %> <% end %> </div> <div class="chatboxinput"> <%= form_for([@conversation, @message], :remote => true) |f| %> <%= f.text_area :body, class: "chatboxtextarea", "data-cid" => @conversation.id %> <%= f.submit " send", class: "btn btn-primary btn-xs"%> <% end %> </div>
message controller
class messagescontroller < applicationcontroller before_filter :authenticate_user! skip_before_filter :verify_authenticity_token, only: [:create] def create @conversation = conversation.find(params[:conversation_id]) @message = @conversation.messages.build(message_params) @message.user_id = current_user.id @message.save! @path = conversation_path(@conversation) end private def message_params params.require(:message).permit(:body) end end
message's message partial
#_message.html.erb <li class="<%= self_or_other(message) %>"> <div class="avatar"> <img src="http://placehold.it/50x50" /> </div> <div class="chatboxmessagecontent"> <p><%= message.body %></p> <time datetime="<%= message.created_at %>" title="<%= message.created_at.strftime("%d %b %y @ %i:%m%p") %>"> <%= message_interlocutor(message).username %> <%= message.created_at.strftime("%h:%m %p") %> </time> </div> </li>
message's create view
#create.js.erb <% publish_to @path %> var id = "<%= @conversation.id %>"; var chatbox = $(".chatboxcontent"); var sender_id = "<%= @message.user.id %>"; var reciever_id = $('meta[name=user-id]').attr("content"); chatbox.append("<%= j render( partial: @message ) %>"); chatbox.scrolltop(chatbox[0].scrollheight); if (sender_id != reciever_id) { chatbox.chatwith(id); chatbox.children().last().removeclass("self").addclass("other"); chatbox.scrolltop(chatbox[0].scrollheight); chatbox.notify(); } <% end %>
on message controller, create action above code results "missing template" , if put respond_to as:
respond_to |format| format.js { render 'create.js.erb' } end
it results "unknown format"
this error occurs when have not added jquery_ujs
file. included jquery file.
so need add both files manually in view or require them in application.js
or other file using specific layout.
depending upon scenario, can follow 1st or 2nd solution.
1st solution:
<%= javascript_include_tag :jquery, :jquery_ujs %>
on conversation show page.
2nd solution:
remove "layout false"
on conversation controller
Comments
Post a Comment