我的消息没有显示在我的显示页面上
my messages aren't displaying on my show page
我正在制作一个简单的聊天室应用程序,我遇到的问题是当我检查 rails 控制台时,我的消息没有进入数据库。当我按回车键提交我的表单时,页面重新加载但没有显示任何消息。我在 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
由于空白 user_id
.
导致的验证错误,消息未保存在数据库中
在 MessagesController
的 create
操作中检查这一行:
message.user == current_user
应该是:
message.user = current_user
第一个是检查 message.user
是否等于 current_user
;第二个 将 current_user
分配给 message_user
.
正确设置后message.user
消息将保存在数据库中。
我正在制作一个简单的聊天室应用程序,我遇到的问题是当我检查 rails 控制台时,我的消息没有进入数据库。当我按回车键提交我的表单时,页面重新加载但没有显示任何消息。我在 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
由于空白 user_id
.
在 MessagesController
的 create
操作中检查这一行:
message.user == current_user
应该是:
message.user = current_user
第一个是检查 message.user
是否等于 current_user
;第二个 将 current_user
分配给 message_user
.
正确设置后message.user
消息将保存在数据库中。