2017-06-17 117 views
-1

我正在制作一个简单的聊天室应用程序,我遇到的问题是当我检查导航控制台时,我的消息没有打到数据库。当我按Enter键提交我的表单时,页面重新加载,但没有消息显示。我允许messagesController params.require(:message).permit(:body)中的消息params,所以我不明白为什么我的消息没有显示出来。我的消息没有显示在我的显示页面

messages_controller.rb

class MessagesController < ApplicationController 
    before_action :authenticate_user! 
    before_action :set_chatroom 

    def create 
    message = @chatroom.messages.new(message_params) 
    message.user == current_user 

    message.save 
    redirect_to @chatroom 
    end 

    private 

    def set_chatroom 
     @chatroom = Chatroom.find(params[:chatroom_id]) 
    end 

    def message_params 
     params.require(:message).permit(:body) 
    end 
end 

show.html.erb

<p> 
    <strong>Name:</strong> 
    <%= @chatroom.name %> 
</p> 

<div data-behavior='messages'> 
    <% @chatroom.messages.order(created_at: :desc).limit(100).reverse.each do |message| %> 
    <div><strong><%= message.user.username %></strong> <%= message.body %></div> 
    <% end %> 
</div> 

<%= form_for [@chatroom, Message.new] do |f| %> 
    <%= f.text_area :body, rows: 1, class: "form-control", autofocus: true %> 
    <%= f.submit %> 
<% end %> 

message.rb

class Message < ApplicationRecord 
    belongs_to :chatroom 
    belongs_to :user 
end 

chatrooms_controller.rb

class ChatroomsController < ApplicationController 
    before_action :set_chatroom, only: [:show, :edit, :update, :destroy] 

    # GET /chatrooms 
    # GET /chatrooms.json 
    def index 
    @chatrooms = Chatroom.all 
    end 

    # GET /chatrooms/1 
    # GET /chatrooms/1.json 
    def show 
    end 

    # GET /chatrooms/new 
    def new 
    @chatroom = Chatroom.new 
    end 

    # GET /chatrooms/1/edit 
    def edit 
    end 

    # POST /chatrooms 
    # POST /chatrooms.json 
    def create 
    @chatroom = Chatroom.new(chatroom_params) 

    respond_to do |format| 
     if @chatroom.save 
     format.html { redirect_to @chatroom, notice: 'Chatroom was successfully created.' } 
     format.json { render :show, status: :created, location: @chatroom } 
     else 
     format.html { render :new } 
     format.json { render json: @chatroom.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /chatrooms/1 
    # PATCH/PUT /chatrooms/1.json 
    def update 
    respond_to do |format| 
     if @chatroom.update(chatroom_params) 
     format.html { redirect_to @chatroom, notice: 'Chatroom was successfully updated.' } 
     format.json { render :show, status: :ok, location: @chatroom } 
     else 
     format.html { render :edit } 
     format.json { render json: @chatroom.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /chatrooms/1 
    # DELETE /chatrooms/1.json 
    def destroy 
    @chatroom.destroy 
    respond_to do |format| 
     format.html { redirect_to chatrooms_url, notice: 'Chatroom was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_chatroom 
     @chatroom = Chatroom.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def chatroom_params 
     params.require(:chatroom).permit(:name) 
    end 
end 

chatroom_users_controller.rb

class ChatroomUsersController < ApplicationController 
    before_action :authenticate_user! 

    before_action :set_chatroom 

    def create 
     @chatroom_user = @chatroom.chatroom_users.where(user_id: current_user.id).first_or_create 
     redirect_to @chatroom 
    end 

    def destroy 
     @chatroom_user = @chatroom.chatroom_users(user_id: current_user.id).destroy_all 
    redirect_to chatrooms_path 
    end 

    private 

    def set_chatroom 
     @chatroom = Chatroom.find(params[:chatroom_id]) 

    end 
end 
+0

控制台中是否有任何消息? –

+0

@SebastiánPalma在控制台中没有任何消息,所有控制台都说'Message.all 消息加载(16.1ms)SELECT“messages”。* FROM“messages” =># jmike

回答

3

消息不被保存在数据库中,因为所造成的空白user_id验证错误的。

检查该行中create动作的MessagesController

message.user == current_user 

它应该是:

message.user = current_user 

第一个是检查是否message.user等于current_user;第二个分配current_usermessage_user

一旦你正确设置了message.user该消息将被保存在数据库中。

+0

谢谢你的工作! @gerry – jmike