我很难弄清楚我的代码中出了什么问题。基本上,我正在使用Dropzone-js's
文件上传器,它使用拖放功能。我无法上传文件,因为它会返回404 error
。
我搜索了我的日志,发现controller
函数update
中存在问题。
控制器:
class ChatRoomsController < ApplicationController
before_action :authenticate_user!
before_action :set_room, only: [:index, :new, :create, :show, :edit, :signal]
before_action :set_participant, only: [:signal]
def index
@chat_rooms = ChatRoom.all
end
def show
# Show room messages
@chat_room = ChatRoom.includes(:messages).find_by(id: params[:id])
@message = Message.new
@chat_room.participant?(current_user)
# TODO: get participant only once
if params[:guid]
if @participant = User.find(params[:guid])
@participant.joined_at = Time.now
@chat_room.add_to_call(@participant)
end
elsif params[:del]
if @participant = User.find(params[:del])
@chat_room.remove_from_call(@participant)
end
end
response = {
room: @chat_room,
# Get all call participants
users: @chat_room.call_users,
signals: deliver_signals!
}
respond_to do |format|
format.html
format.json { render json: response }
end
end
def new
@chat_room = ChatRoom.new
end
def edit
# Empty
end
def create
@chat_room = current_user.chat_rooms.build(chat_room_params)
if @chat_room.save
@group_room.users << current_user
redirect_to chat_rooms_path
else
render 'new'
end
end
def update
@chat_room = ChatRoom.find(id: params[:chat_room_id])
if @chat_room.update_resource(chat_room_params)
flash[:success] = 'test'
else
render 'edit'
end
end
def signal
signal = signal_params
signal[:chat_room] = @chat_room
signal[:sender] = User.find(signal[:sender])
signal[:recipient] = @participant
logger.info('Signal is ' + signal.to_param)
ChatRoomSignal.create! signal
head 204
end
def deliver_signals!
data = ChatRoomSignal.where recipient: @participant
# Destroy the signals as we return them, since they have been delivered
result = []
data.each do |signal|
result << {
signal_type: signal.signal_type,
sender_guid: signal.sender_id,
recipient_guid: signal.recipient_id,
data: signal.data,
chat_room_id: signal.chat_room_id,
timestamp: signal.created_at
}
end
data.delete_all
result
end
private
def set_participant
@participant = User.find(params[:user_id])
rescue ActiveRecord::RecordNotFound
# Retry with ID as GUID
@participant = User.where(id: params[:user_id]).first
raise unless @participant
end
def set_room
@chat_room = ChatRoom.includes(:messages).find_by(id: params[:chat_room_id])
end
def chat_room_params
params.require(:chat_room).permit(:title, :image)
end
def signal_params
params.permit(:sender, :signal_type, :data)
end
用于文件上传的HTML代码:
<div class="panel-body">
<%= form_for @chat_room, html: { multipart: true, class: "dropzone", id: "my-dropzone"} do |f| %>
<div class="dz-message needsclick">
<h3>Drop a file here</h3> or <strong>click</strong> to upload
</div>
<div class="fallback">
<% f.file_field :image, as: :file %>
<%= f.submit "Upload your file" %>
</div>
<% end %>
</div>
错误:
[ActionCable] [test@test.com] ChatRoomsChannel is transmitting the subscription confirmation
[ActionCable] [test@test.com] ChatRoomsChannel is streaming from chat_rooms_1_channel
Started PATCH "/chat_rooms/1" for 127.0.0.1 at 2017-06-09 01:44:26 +0200
Processing by ChatRoomsController#update as JSON
Parameters: {"utf8"=>"✓", "authenticity_token"=>"p8KEWBx7fmJmEhHgINmp5rnj+PVwGXfbPHxslSaA4Z/5zA6HIJzxeBjwcz/+GcDEQKKwPwjXNJVnBtfq7xu2qw==", "chat_rooms"=>{"image"=># <ActionDispatch::Http::UploadedFile:0x007f640e58f5b0 @tempfile=#<Tempfile:/tmp/RackMultipart20170609-2887-1nuat54.png>, @original_filename="Screenshot from 2017-04-12 12-47-21.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"chat_rooms[image]\"; filename=\"Screenshot from 2017-04-12 12-47-21.png\"\r\nContent-Type: image/png\r\n">}, "id"=>"1"}
[1m[36mUser Load (0.2ms)[0m [1m[34mSELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ?[0m [["id", 1], ["LIMIT", 1]]
[1m[36mChatRoom Load (0.2ms)[0m [1m[34mSELECT "chat_rooms".* FROM "chat_rooms" WHERE "chat_rooms"."id" = ? LIMIT ?[0m [["id", nil], ["LIMIT", 1]]
Completed 404 Not Found in 7ms (ActiveRecord: 0.3ms)
ActiveRecord::RecordNotFound (Couldn't find ChatRoom with 'id'={:id=>nil}):
app/controllers/chat_rooms_controller.rb:62:in `update'
第62行:
@chat_room = ChatRoom.find(id: params[:chat_room_id])
所以我的controller
似乎无法找到id
参数,但我不明白为什么。这可能是导致我的文件返回404 error
的错误。
感谢您抽出时间阅读我的帖子。
答案 0 :(得分:1)
您使用了错误的密钥来获取id
,请尝试使用params[:id]
:
@chat_room = ChatRoom.find(params[:id])
另请注意,id:
已被删除,因为find
会查找作为参数提供的id
。
此外,您应该更新chat_room_params
方法:
def chat_room_params
params.require(:chat_rooms).permit(:title, :image)
end
由于您只更新了2个属性,因此可以像这样重构update
方法:
def update
@chat_room = ChatRoom.find(id: params[:chat_room_id])
@chat_room.title = chat_room_params[:title]
@chat_room.image = chat_room_params[:image]
if @chat_room.save(chat_room_params)
flash[:success] = 'test'
else
render 'edit'
end
end
答案 1 :(得分:0)
您的params[:chat_room_id]
似乎是哈希..
尝试
@chat_room = ChatRoom.find(id: params[:chat_room_id][:id])