我正在编写一个我正在处理的rails项目中的聊天页面。我在聊天窗口旁边有一个侧栏,因此用户可以轻松地在对话之间切换。我一直想用ajax重新实现页面,但在我做之前我需要解决一个bug。当我单击其中一个侧边栏用户按钮时,会话发生变化,当我单击我之前再次说话的用户按钮时,会话无法切换。我有一个我定义为翻牌的动作,当被叫时改变了对话的接收用户。就像我说的那样,我可以为每个用户调用一次动作,但是当尝试返回已经查看过的对话时,显示不会改变。
这是控制器:
class ChatsController < ApplicationController
$messeges = Array.new(10, " ")
def new
$messeges = Array.new
if $ruser != nil
$messeges = Array.new
Chat.all.each_with_index do |i, index|
if i.recipient == current_user.id && i.sender == $ruser.id
$messeges.push([i.content, "to"])
end
if i.recipient == $ruser.id && i.sender == current_user.id
$messeges.push([i.content, "from"])
end
end
end
$chat_users = Array.new
User.all.each do |user|
if user != nil && current_user != nil
if user.game_id == current_user.game_id && user.id != current_user.id
$chat_users.push(user.username)
end
end
end
@chat = Chat.new
end
def create
if $ruser != nil
@chat = Chat.new(chat_params)
@chat.recipient = $ruser.id
@chat.sender = current_user.id
@chat.save
end
redirect_to "/comms/new"
end
def flop
$ruser = nil
$ruser = User.find(params[:id])
redirect_to "/comms/new"
end
private
def chat_params
params.require(:chat).permit(:content)
end
end
这是新动作的视图,我在这里调用flop:
<!DOCTYPE html>
<html>
<head>
<title>Battleriskopoloy - Communications</title>
</head>
<body>
<br>
<div id="heading_1" align="center">
<br><span id="title"><strong>[ . . . Communications . . . ]</strong></span>
</div><br>
<div id="sidebar"><br>
<% $chat_users.each do |user| %>
<% user = User.find_by_username(user) %>
<%= button_to user.username, flop_chat_path(user), :class => "select", :method => :get %>
<% end %>
</div>
<div id="display">
<% $messeges.each do |i| %>
<% if i[1] == "from" %>
<p style="color:#000000; text-align:right; margin-right:5%;font-family:courier"><%= i[0] %></p>
<br>
<% else %>
<p style="color:#FF0000; text-align:left; margin-left:5%; font-family:courier"><%= i[0] %></p>
<br>
<%end%>
<%end%>
</div>
<div id="textfield">
<%= form_for(@chat) do |f| %>
<%= f.text_field :content, id: "compose" %>
<br>
<br>
<%= f.submit "Send Messege", id: "submit" %>
<% end %>
</div>
</body>
</html>
关于这涉及的可能性,这是我的routes.rb:
Rails.application.routes.draw do
resources :sessions, only: [:new, :create, :destroy]
resources :users
resources :games
resources :forts
resources :chats do
member do
get :flop
end
end
match '/forts/create', to: 'forts#create', via: 'post'
match '/comms/new', to: 'chats#new', via: 'get'
match '/comms', to: 'chats#show', via: 'get'
match '/join_game', to: 'users#edit', via: 'get'
match '/new_game', to: 'games#new', via: 'get'
match '/signup', to: 'users#new', via: 'get'
match '/signin', to: 'sessions#new', via: 'get'
match '/signout', to: 'sessions#destroy', via: 'delete'
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
root 'home#home'
# Example of regular route:
# get 'products/:id' => 'catalog#view'
# Example of named route that can be invoked with purchase_url(id: product.id)
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
# Example resource route (maps HTTP verbs to controller actions automatically):
# resources :products
# Example resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Example resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Example resource route with more complex sub-resources:
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', on: :collection
# end
# end
# Example resource route with concerns:
# concern :toggleable do
# post 'toggle'
# end
# resources :posts, concerns: :toggleable
# resources :photos, concerns: :toggleable
# Example resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
end
非常感谢你的帮助,
亚历