简体   繁体   English

Ruby on Rails ,:注意

[英]Ruby on Rails, :notice

I am using this file uploader example for Ruby on Rail . 我正在使用Ruby on Rail的文件上传器示例

I have this piece of code in my controller. 我的控制器中有这段代码。 And I need to have a :notice parameter somewhere, so when the file is uploaded the notice will be "You have uploaded a file", if there is a error then "Something went wrong" 而且我需要在某处有一个: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

So, I need something like this: 所以,我需要这样的东西:

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

but I have no idea how to integrate the :notice into my code 但是我不知道如何将:notice集成到我的代码中

Thanks in advance. 提前致谢。

When you say 'integrate', do you mean how I can use the value of notice in view or the controller method? 当您说“集成”时,您是说我如何在视图或控制器方法中使用注意值?

If so, you can just use params[:notice] to get the value in your view or the controller you are redirecting to. 如果是这样,您可以只使用params [:notice]在视图或您要重定向到的控制器中获取值。

This is how you 'integrate' the notice to your responses 这是您将通知“整合”到响应中的方式

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM