Chris Mendla
Chris Mendla

Reputation: 1017

Trying to assign contents of a variable to params[:

I need to grab the contents of a form before the submit key is pressed in order to save the user input. If the form is submitted and validation fails then the user input is lost.

I have the main contents of the form working ok with session variables. However I want to take the params[:images] from paperclip and save that to a column in a table and then load it back into params[:images]

My form has

<div class="field" title="You may attach one or more documents to this observation: Pictures, word documents, spreadsheets, etc">
  <%= f.label :Upload_attachments %><br>
  <%= file_field_tag "images[]", type: :file,  multiple: true %>
</div>

in the respond_to for create, I can write the contents of params[:images] to a table with

 if params[:images]
      temp_data = TempDatum.create( item: '2',  data: params[:images])
    end

That works and results in something like the following in data

[#, @original_filename="2016 06 13 capture.JPG", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"images[]\"; filename=\"2016 06 13 capture.JPG\"\r\nContent-Type: image/jpeg\r\n">]

in my create, I have the following

 @image_data = TempDatum.last
     if @image_data.present?
       # params[:images] = @image_data.data
       params[:images] =  '[#<ActionDispatch::Http::UploadedFile:0xe4a46e8 @tempfile=#<Tempfile:C:/Users/cmendla/AppData/Local/Temp/RackMultipart20160614-13124-iib9h0.JPG>, @original_filename="2016 06 13 capture.JPG", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"images[]\"; filename=\"2016 06 13 capture.JPG\"\r\nContent-Type: image/jpeg\r\n">]'
       $temp1 = 'got here'
     end

I have tried various ways of trying to get the contents of TempDatum.data into the params[:images] The way it is now, I was simply trying to take the raw data from the data field in the table.

What do I need to do to set params[:images] to the values in TempDatum.data for a given record.

Upvotes: 0

Views: 83

Answers (1)

tagCincy
tagCincy

Reputation: 1599

What you are attempting to do will not work and is grossly over-engineered. The params object is a ActionController::Parameters, therefore, it will never be accessible outside the context of the controller that has created it (can't access it in the view).

Here is what you need to do:

First, make sure you are instantiating the object you are attempting to create in the new action and make sure you map your form to that object.

Second, you need to instantiate the object again at the beginning of your create action. You should then have a conditional to the save action of that object. If the save fails due to validation, render :new and the form will hold the inputted data.

def new
    # map this to your form_for
    @object = Model.new
end

def create
    @object = Model.new(params)
    #  other logic...
    if @object.save
        # do what you need to do if everything is valid...
    else
        render :new
    end
end

Upvotes: 1

Related Questions