This week, I found two things that I couldn’t do without. Well, I suppose I could, but I would spend a whole lot more time writing code
than I would have in the past.
The first tool i found was Streamlined. The trick with this is that it
allows you to simply define your models and their relationships, and
quickly build admin interfaces for them. While I am not sure if it is
the quickest way to build forms for general use, it really smacks an
admin interface around.
The other tool I found was UploadColumn. The trick here is that you
can quickly and easily upload files to your app. The files are deleted
when the object is deleted, you can use ‘magic’ columns to save file
sizes, mime types, and a whole bunch of other useful info.
The problem is, I needed both functions, but there was no really easy
way that I could see to do it. There were parts all over the net, but
nothing really worked. So.. I hit it for a bit, and hopefully someone
can save some time from my toils.
To get these pieces working, you have to do the following:
-
Add a column to your model for the file. You should probably do
this to your migrations.
-
At the very least, add this to your model:
uploadcolumn :file
See the documentation for full details on how this works. There are
lots of other settings you can hit here.
-
from vendors/plugins/streamlined/templates/genericviews, copy
form.rhtml, new.rhtml, and edit.rhtml to the view direcotry of the
model you are working with.
-
Find the spot in form.html that generates the listing, and put
this in there:
You will also need to change the block so that that will only be- <tr id="slfieldmedia">
- <td class="sleditlabel"><label for=<%="#{form.objectname}file"%>><%= column.humanname %></label></td>
- <td class="sleditvalue"><%= uploadcolumnfield form.objectname, 'file' %></td>
- </tr>
evaluated when you hit the file part of the form. The whole thing
will look like this:
;- <% modelui.editcolumns.each do |column| %>
- <% if column.name.tosym == :file %>
- <tr id="sl_field_media">
- <td class="sleditlabel"><label for=<%="#{form.objectname}file"%>><%= column.humanname %></label></td>
- <td class="sleditvalue"><%= uploadcolumnfield form.objectname, 'file' %></td>
- </tr>
- <% elsif column.isdisplayableincontext?(self, @streamlineditem) %>
- <%= column.rendertredit(self, @streamlineditem) %>
- <% end %>
- <% end %>
-
In both new.rhtml and edit.rhtml, you have to remove the call to
the ajax form. For some reason, this doesn’t work in ajax. Replace
this:
; This will turn off ajax, and produce a multipart form.- <% if modelui.newsubmitbutton[:ajax] %>;
- <% options[:html] = {:onsubmit=>;'Streamlined.Form.submit(this); return false;'} %>;
- with this:
- <% options[:html] = {:multipart =>; true} %>;
- This should work!
The problem I was running into was getting the ajax turn off but still
produce a multipart form. Step five is where the real magic happens.
Let me know if i missed anything.
