我正在努力让AJAX在我的应用程序上工作,我觉得好像我接近终点线,但我所拥有的最终结果并不完全是我想要的。
我正在关注一个聊天教程,您可以找到here,但稍微调整一下以使其可以在应用程序的任何页面上使用。我的聊天功能如何;
我遇到的问题是;当用户在对话中键入消息时,在按Enter键时,它给了我一个:
Template is Missing
Missing template messages/create, application/create with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in: * "/home/gerardo/667-workspace/Term-H/app/views" * "/home/gerardo/.rvm/gems/ruby-2.2.1/gems/devise-3.5.2/app/views"
在终端中,会发生这种情况:
Started POST "/conversations/1/messages" for 127.0.0.1 at 2015-12-03 19:04:01 -0800
Processing by MessagesController#create as HTML
Parameters: {"utf8"=>"✓", "message"=>{"body"=>"Video"}, "conversation_id"=>"1"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Conversation Load (0.6ms) SELECT "conversations".* FROM "conversations" WHERE "conversations"."id" = $1 LIMIT 1 [["id", 1]]
(0.2ms) BEGIN
SQL (0.4ms) INSERT INTO "messages" ("body", "conversation_id", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["body", "Video"], ["conversation_id", 1], ["user_id", 1], ["created_at", "2015-12-04 03:04:01.087945"], ["updated_at", "2015-12-04 03:04:01.087945"]]
(4.8ms) COMMIT
Completed 500 Internal Server Error in 16ms (ActiveRecord: 6.2ms)
我在StackOverflow周围搜索并看到一些人建议在MessageController中将我的代码添加到我的创建操作中。
respond_to do |format|
format.js
end
添加此内容后,我会转到空白页面,浏览器上的网址为:http://localhost:3000/conversations/1/messages 终端显示:
Started POST "/conversations/1/messages" for 127.0.0.1 at 2015-12-03 19:21:24 -0800
Processing by MessagesController#create as JS
Parameters: {"utf8"=>"✓", "message"=>{"body"=>"Please..."}, "conversation_id"=>"1"}
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Conversation Load (0.2ms) SELECT "conversations".* FROM "conversations" WHERE "conversations"."id" = $1 LIMIT 1 [["id", 1]]
(0.1ms) BEGIN
SQL (0.2ms) INSERT INTO "messages" ("body", "conversation_id", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["body", "Please..."], ["conversation_id", 1], ["user_id", 1], ["created_at", "2015-12-04 03:21:24.358502"], ["updated_at", "2015-12-04 03:21:24.358502"]]
(5.0ms) COMMIT
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 2]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
Rendered messages/_message.html.erb (2.8ms)
Rendered messages/create.js.erb (22.1ms)
Completed 200 OK in 39ms (Views: 23.9ms | ActiveRecord: 6.9ms)
的routes.rb
Rails.application.routes.draw do
devise_for :users, :path_names => {:sign_in => "login"}, :controllers => {omniauth_callbacks: "omniauth_callbacks"}
# Root Page
root to: 'pages#home'
#Video routes
get 'video/index'
get 'video/profile'
#Pages routes
get 'pages/home'
get 'pages/subjects'
get 'pages/profile'
get 'pages/featured'
#Suggestion routes
resources :suggestions
#Chat routes
resources :users
resources :conversations do
resources :messages, defaults: {format: 'js'}
end
end
home.html.erb(用户点击聊天,否则隐藏)
<!-- Chat
==========================================-->
<% if user_signed_in? %>
<div class="chat-menu">
<!-- Menu icon -->
<a class="menu-close">Close</a>
<!-- Menu -->
<ul>
<% @users.each do |user| %>
<li>
<tr>
<td><%= user.firstname %> <%= user.lastname %></td>
<td>
<%= link_to "Send Message", "#", class: "btn btn-success btn-xs start-conversation", "data-sid" => current_user.id, "data-rip" => user.id %>
</td>
</tr>
</li>
<% end %>
</ul>
</div>
<% end %>
控制上面div滑动进/出动画的JavaScript
$('#chat-toggle').click(function() {
$('.chat-menu').animate( {
right: '0px'
}, 200);
});
$('.menu-close').click(function() {
$('.chat-menu').animate( {
right: '-285px'
}, 200);
});
}
messages_controller.rb
class MessagesController < ApplicationController
before_filter :authenticate_user!
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)
respond_to do |format|
format.js
end
end
private
def message_params
params.require(:message).permit(:body)
end
end
如果还有其他需要,请告诉我。如果需要,我还可以为上述三个步骤提供视觉效果。