LondonGuy
LondonGuy

Reputation: 11098

How do I stop a hidden_field from converting my file value into a string?

So I've debugged the value from my parse.com database like this:

<%= debug @activity["image1"] %>

Result in view shows it is a file as I expect:

--- !ruby/object:Parse::File
parse_filename: tfss-1c325650-79d2-4d34-ab3b-c62397c7ac9a-image1.jpg
url: http://files.parsetfss.com/0b53rerr13f-42e5-be7ac9a-image1.jpg

I try to add it as a value of my form like this:

<%= form_for :leads, :url => activities_create_path do |f| %>
<%= f.hidden_field :image, :value => @activity["image1"] %>
<% end %>

In my controller I try to save like this:

def create
    @lead = Leads.new(params[:leads])
    if @lead.valid?
        lead = Parse::Object.new("Leads")

        lead["image"] = params[:leads][:image]

        lead.save
        redirect_to root_url
    end
end

The error I'm getting tells me that it can't save because it was expecting a file but got a string.

I know the value is a file but I suspect hidden_fields values end up being strings.

  1. Is there a way to convert a hidden_field value to a string?

  2. If question 1 isn't possible then can I create a file_field and set it's value without manually uploading a image file?

What I actually need to do is take the default image file of an object from my parse.com database and store it exactly as it is (a file) back into another table in parse.com.

The problem I'm having is in my rails form where the file is being converted to a string. Another option I have is to some how grabbed the same image which is actually being displayed at the top of the page where the form.

This is how I display the image from the file:

<%= image_tag @activity["image1"].url, onerror:"this.style.display='none'" if @activity["image1"].present? %>

Then some how use that image to simulate adding a file to a file_field. However I think that is slightly complicated and unnecessary for what I'm trying to do.

I have a file from one table in parse.com and I read it in rails but now want to take that file and do nothing to it, then store it back into parse.com but in another table. However as I mentioned above hidden_field converts the file to a string but I need it to remain in it's same format.

I hope this all makes sense.

Would appreciate your help.

Thanks for your time.

Upvotes: 1

Views: 105

Answers (1)

thaleshcv
thaleshcv

Reputation: 752

Http parameters and html fields values will always be like a string. Try to store the value of 'parse_filename' or 'url' from your Parse::File in the hidden field. In the action method you can read the file using the path submitted.

Upvotes: 1

Related Questions