Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 上载文件时出现错误404,原因是更新功能出错_Ruby On Rails_Carrierwave_Dropzone.js - Fatal编程技术网

Ruby on rails 上载文件时出现错误404,原因是更新功能出错

Ruby on rails 上载文件时出现错误404,原因是更新功能出错,ruby-on-rails,carrierwave,dropzone.js,Ruby On Rails,Carrierwave,Dropzone.js,我很难找出代码中的错误。基本上,我使用的是Dropzone js的文件上传程序,它使用拖放。我无法上载文件,因为它返回了一个404错误。 我搜索了我的日志,发现我的控制器在更新功能中有问题 控制器: class ChatRoomsController < ApplicationController before_action :authenticate_user! before_action :set_room, only: [:index, :new, :create, :show, :

我很难找出代码中的错误。基本上,我使用的是Dropzone js的文件上传程序,它使用拖放。我无法上载文件,因为它返回了一个
404错误
。 我搜索了我的日志,发现我的
控制器
更新
功能中有问题

控制器

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
<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>
@chat_room = ChatRoom.find(id: params[:chat_room_id])
因此,我的
控制器
似乎无法找到
id
参数,但我不明白为什么。这可能是导致我的文件返回
404 error
的错误


感谢您花时间阅读我的帖子。

您的
参数[:chat\u room\u id]
似乎是个杂凑

试一试


您使用错误的键获取
id
,请尝试使用
params[:id]

@chat_room = ChatRoom.find(params[:id])
还要注意,
id:
已被删除,因为
find
将查找作为参数提供的
id

此外,您应该更新您的
聊天室参数
方法:

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

日志显示了什么?检查参数,很可能是您在
chat\u room\u id
键中没有得到任何值。更新的错误日志生成了一个“NoMethodError(未定义的方法“[]”,用于nil:NilClass):”,似乎与“[:id]”不一样@Gerry不起作用:ActiveRecord::RecordNotFound(找不到具有'id'=”的聊天室):好的,所以您似乎发现了什么。我现在有一个新错误:ActiveRecord::RecordNotFound(找不到id为“={:id=>”1“}”的聊天室):但我真的不明白,我当前的聊天室id等于1,所以我不明白为什么它搜索id=>1@xZeasy检查更新的答案,从查找中删除
id:
,这导致了错误。还有另一个新错误:ActionController::ParameterMissing(param丢失或值为空:chat_room):现在我有一个错误400错误请求,不再是404。它是ligne 115“app/controllers/chat_rooms_controller.rb:115:'chat_room_params'中的。这就是聊天室参数所在的位置,聊天室是必需的parameter@xZeasy我现在明白了,您必须使用
require(:chat\u rooms)
更新您的
聊天室参数。
def chat_room_params
 params.require(:chat_rooms).permit(:title, :image)
end
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