0

我正在为 Ruby on Rail 使用这个文件上传器示例

我的控制器中有这段代码。而且我需要在某处有一个 :notice 参数,所以当文件上传时,通知将是“您已上传文件”,如果有错误则“出现问题”

def create
    p_attr=params[:upload]
    p_attr[:arraydb] = params[:upload][:upload].first if params[:upload][:upload].class == Array
    @upload = Upload.new(p_attr)

    respond_to do |format|
      if @upload.save
        @upload.update_attributes(:user_id => current_user.id)
        format.html {
          render :json => [@upload.to_jq_upload].to_json,
          :layout => false

        }
        
        format.json { render json: [@upload.to_jq_upload].to_json, status: :created, location: @upload }
      else
        
        format.html { render action 'new' }
        format.json{ render json: {name:(@upload.upload_file_name).split(".").first ,error: @upload.errors.messages[:upload_file_name]}, :status =>422}
      end
    end
  end

所以,我需要这样的东西:

format.html { redirect_to(@upload, :notice => "LALALALALALA") }

但我不知道如何将 :notice 集成到我的代码中

提前致谢。

4

2 回答 2

0

当您说“集成”时,您的意思是我如何在视图或控制器方法中使用通知的值?

如果是这样,您可以使用 params[:notice] 来获取视图中的值或要重定向到的控制器。

于 2013-02-15T19:18:31.610 回答
0

这就是您将通知“整合”到您的回复中的方式

def create
  p_attr=params[:upload]
  p_attr[:arraydb] = params[:upload][:upload].first if params[:upload][:upload].class == Array
  @upload = Upload.new(p_attr)

  respond_to do |format|
    if @upload.save
      @upload.update_attributes(:user_id => current_user.id)
      format.html { redirect_to(@upload, :notice => "Success") }
      format.json { render json: [@upload.to_jq_upload].to_json, status: :created, location: @upload }
    else
      format.html { render action 'new', :notice => "Failed" }
      format.json{ render json: {name:(@upload.upload_file_name).split(".").first ,error: @upload.errors.messages[:upload_file_name]}, :status =>422}
    end
  end
end
于 2013-02-16T09:48:51.447 回答