简体   繁体   中英

Ruby on Rails, :notice

I am using this file uploader example for 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"

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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