Attachment data is nil for me - rails app

107 views
Skip to first unread message

kathleen....@gmail.com

unread,
Jul 29, 2016, 1:17:56 PM7/29/16
to Shrine
Hi, 
This is probably a "noob" question.

My plans is to use Shrine to handle xml file uploads in a rails app I'm working on. I'm stuck at the very end of the "Quick Start" section of Shrine's README.md guide. After I submit the form with the (xml) attachment, I'm expecting to see a hash in the :xml_data column, but I'm getting nil. I'm using pry to do this from the controller action the form is submitted to. And I've double checked that all my steps match the guide. I realize this is not much info go off of, so I'll provide all the steps I've followed. I can definitely send code if it is helpful.

The table I'm adding the attachments to is the Uploads table.

Using Rails 4.2.5, Ruby 2.2.5

Here is what I've done:
1. Added the shrine gem and ran bundle - no issues here. 
2. Added the the initializer - in config/initializers/shrine.rb (just copied and pasted from the guide and changed the plugin from sequel to :activerecord)
3. Created a migration to add a text column called :xml_data to my Upload table and ran it - no issues there. Schema looked correct afterwards. Restarted server.
4. Added an XmlUploader class than inherits from Shrine - I created this in a file called xml_uploader.rb under models
5. Added Include XmlUploader[:xml]  within my Upload class (inherits from ActiveRecord::Base)
6. Added the hidden field to my upload form. I noticed that both the hidden field and the exposed field have the same name in the guide (:image), so I followed suit and named both of mine :xml. I also tried naming the hidden one something different (:xml_file) just to see if it made any difference and it didn't, so I put it back.

What am I missing here? Ideas?
-Kathleen


Janko Marohnić

unread,
Jul 29, 2016, 1:46:23 PM7/29/16
to kathleen....@gmail.com, Shrine
Hi Kathleen,

Thank you for the detailed steps, it's super helpful in debugging.

Did you by any chance put the hidden field after the file field? Also, did you perhaps get an error when you changed the hidden field from "xml" to "xml_file"? Because if you didn't, that means the parameter was never sent, because otherwise it would throw a NoMethodError, since this column doesn't exist (as far as I understood). In that case it might be that you didn't whitelist the attachment parameters wih strong_parameters in your controller (although I maybe that would raise an error).

Kind regards,
Janko


--
You received this message because you are subscribed to the Google Groups "Shrine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-shrine...@googlegroups.com.
To post to this group, send email to ruby-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ruby-shrine/d21e5d1f-e4ac-40a0-9603-b702529f77ab%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

kathleen....@gmail.com

unread,
Jul 30, 2016, 9:05:40 AM7/30/16
to Shrine
Thanks, Janko!

It turns out the issue was with my interpretation of the guide. At the end of the end of the Quick Start section, there is a line that says 
Now when a Photo is created with the image attached, you can get the URL to the image:
<img src="<%= @photo.image_url %>">

But at this point, we haven't attached anything yet, so @photo.image_url (in my case @upload.xml_url) would, and should, be nil. The next section covers Attachments. I was following the guide very chronologically, and thought the only possible way for the data to get attached was through some tricky Shrine logic that reads the hidden value, and attaches the object automatically. I realize this makes no sense, but I was so convinced. When I showed it to another developer, they pointed out how the hidden field is just for reloading cached data in the event of a failed validation or something - which makes way more sense, and is clearly pointed out later in the guide! 

So everything is great now.

Thanks again.
-Kathleen

Janko Marohnić

unread,
Jul 30, 2016, 10:14:22 AM7/30/16
to kathleen....@gmail.com, Shrine
Thank you for your feedback, Kathleen, it is really helpful to me to know how someone understands the documentation. I will try to modify the README so that it's clearer.

If you'll have any more troubles, don't hesitate to ask here :)

Kind regards,
Janko


Reply all
Reply to author
Forward
0 new messages