2017-06-17 6 views
-1

Ich mache eine einfache Chatroom-Anwendung, das Problem, das ich habe ist, dass meine Nachrichten nicht die Datenbank schlagen, wenn ich Schienen Konsole überprüfen. Wenn ich Enter drücke, um mein Formular zu senden, wird die Seite neu geladen, aber es wird keine Nachricht angezeigt. Ich habe die Nachricht params im messagesController params.require(:message).permit(:body) erlaubt, so dass ich nicht verstehe, warum meine Nachrichten nicht angezeigt werden.meine Nachrichten werden nicht auf meiner Show-Seite angezeigt

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

Gibt es eine Nachricht in der Konsole? –

+0

@ SebastiánPalma nein es gibt keine Nachricht in der Konsole die ganze Konsole sagen ist "Message.all Message Load (16.1ms) SELECT" Nachrichten ". * FROM" Nachrichten " => # ' – jmike

Antwort

3

Nachrichten nicht durch Leer user_id verursacht aufgrund eines Validierungsfehler in der DB gespeichert werden.

prüfen diese Linie in create Wirkung von MessagesController:

message.user == current_user 

Es sollte:

message.user = current_user 

Die erste ist die Überprüfung, ob message.user gleich current_user ist; die zweite weistcurrent_user zu message_user zu.

Sobald Sie message.user richtig eingestellt haben, wird die Nachricht in der DB gespeichert.

+0

danke es hat funktioniert! @gerry – jmike

Verwandte Themen