2014-06-20 18 views
7

Tôi đang viết một trang trò chuyện cho một trang web RoR. Tôi có tất cả các công việc trong HTML và tôi đang cố gắng thực hiện nó với ajax. Có một biểu mẫu để gửi tin nhắn. tag hình thức đọc <%= form_for(@chat, remote: true, :authenticity_token => true) do |f| %>Dạng Ruby sử dụng ajax với điều khiển từ xa: true cung cấp cho ActionController :: InvalidAuthenticityToken error. Nội dung gửi cổ điển không

toàn bộ quan điểm của tôi:

<!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, remote: true, :authenticity_token => true) do |f| %> 
      <%= f.text_field :content, id: "compose" %> 
      <br> 
      <br> 
      <%= f.submit "Send Messege", id: "submit" %> 
     <% end %> 
     </div> 
    </body> 
</html> 

Bộ điều khiển:

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 = User.find(params[:id]) 
    $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 
    redirect_to "/comms/new" 
end 

private 
    def chat_params 
     params.require(:chat).permit(:content) 
    end 
end 

Ngoài ra, tôi có <%= csrf_meta_tags %> trong application.html.erb tôi

Tôi đã tìm thấy một số những thứ khác trực tuyến về những người nhận được cùng một lỗi với đường ray 4 khi chuyển từ ajax sang trình html nhưng điều đó dường như chỉ vì chúng không có thông số :authenticity_token => true được bao gồm. Bất kỳ trợ giúp nào cũng được đánh giá rất cao.

Trả lời

7

Theo mặc định authenticity_token được đặt thành false. Vì vậy, bạn cần phải thêm dòng này vào số application.rb

config.action_view.embed_authenticity_token_in_remote_forms = true 
+1

Tôi thậm chí không biết nguồn của dự án này hiện đang ở đâu. Tôi nhớ mặc dù tôi đã từ bỏ. Thực sự đánh giá cao câu trả lời mặc dù! – obventio56

+1

@techdreams tại sao đường ray có thuộc tính này false bydefault – wasipeer

Các vấn đề liên quan