small medium large xlarge

Generic-user-small
16 Oct 2008, 00:45
James West (104 posts)

Hi, Like all OO languages it seems that with RoR there are many ways to skin a cat and I would like some advice on the following idea I have had.

What I would like to do is to write a bit of generic code that puts a file_field on a form. The file_field form obviously needs to be multi part as mentioned in the book

I also want a bit of code that does different things with the file_field value if it has been set.

So I have scoured the book for answers as to how this could be done and come up with several possibilities.

The part of the book that talks about file uploading gives this bit of code as an example for the view

<% form_for(:picture, :url => {:action => 'save'},
            :html => { :multipart => true }) do |form| %>
  Comment: <%= form.text_field("comment" ) %><br/>
  Upload your picture: <%= form.file_field("uploaded_picture" ) %><br/>
  <%= submit_tag("Upload file" ) %>
<% end %>

What I would want to do is something like

  Upload your picture: <%= form.my_file_upload_field("uploaded_picture" ) %><br/>

And I would want to be able to do this in any view.

The first thought that sprang to mind was that what I really wanted then was a helper. This helper would need to define a multi part form as it may be used in views that do not have this type of form defined and and as this helper could be used in any view then it would need to live in the application_helper.rb. also taking into account the fact that the helper is not associated with a model then it should be a form_tag rather than a form_for so I came up with something like this in the application_helper.rb

<% form_tag(:something? maybe :my_file_upload_field
                 :html => { :multipart => true }
                 ) do |form|
        <%= form.file_field("uploaded_picture" ) %>
      <%end%> 

This raises questions of what to do with the “uploaded_picture” attribute as this no longer makes sense.

O.K. Accepting then that there are satisfaciry answers to these questions I then need to associate the field with a bit of code that will do something with the value set in the my_file_upload_field. So where to put the bit of code that takes whatever action is needed when the user presses a submit_tag button in whatever view is using this? I would need a controller that deals with this me thinks but now I am starting to get really out of my depth.

Page 514 starts talking about custom form builders and I start thinking that this is what I need instead or maybe it’s the same thing?

There is also mention of working with non model fields on page 519 so now I start thinking that what I really need is some advice on just what it is that I should be doing in order to achoeve my original task.

So any help and pointers would be really appreciated especially when it comes to getting the submit action on whtever form is used to call a function that will behave as I want it to based on parameter arguments.

This sort of task in any of my tradition OO language backgrouds is really trivial to do. I would just create a new class based on something like a TControl. Create a couple of new properties, add a method to the class and probably add an on error event handler.

The form I would use this class in would set the properties of this class in it’s constructor, attach an error event handler call back routine and then when the user presses an appropriate button call the method on the instantiated object to tell it to take whatever action it needs to. Sounds like a lot of work but in reality it’s half a dozen lines of code. Doing this in rails seems to be really complicated which leads me to believe that I have not chosen the right solution and I really know nothing like enough to start cutting proper rails code.

so please help :-)

Cheers

James

Generic-user-small
16 Oct 2008, 09:56
James West (104 posts)

I think on reflection that I’m going to use a helper and a partial template. That way I can share the helper in whichever controllers need access to it.

Having said this I would still appreciate any advice as to whether or not this is the right path to follow

You must be logged in to comment