我正在制作一个简单的聊天室应用程序,我遇到的问题是当我检查rails控制台时,我的消息没有访问数据库。当我按Enter键提交我的表单时页面重新加载但没有显示消息。我在messagesController params.require(:message).permit(:body)
中允许了消息参数,所以我不明白为什么我的消息没有显示出来。
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 :(得分:3)
由于空白user_id
导致验证错误,消息未保存在数据库中。
在create
的{{1}}操作中检查此行:
MessagesController
应该是:
message.user == current_user
第一个是检查message.user = current_user
是否等于message.user
;第二个指定 current_user
到current_user
。
正确设置message_user
后,邮件将保存在数据库中。