← Back to Upcase

CRUD Actions Only in Controller

(Ray Hooker) #1

You mentioned that we should see everything in terms of the restful actions of create, replace, update and delete. So I was thinking back to an app that I just wrote that works with patient records. I have an upload feature to load in data from a zip file. There is a form to gather the file name and an “upload_file” method on the controller. Of course, I had to create a specific route.

So my thoughts were to see this as a type of create for patient records. If the value upload file is found in the params, then the create action would call the upload method, otherwise it would create a single patient record. Does that sound right?

If so, then how best to implement following the new best practices and conventions. For the view, I don’t believe I can do a form_for @patient because I am creating a collection of them.

<%= form_tag("/patients/", :method => “put”, :multipart=>true) do %>

This works and generates a post to patients. I would assume that I would need to pass “fileupload” switch as a hidden field with default value of true such as I am doing with the file name:
<li><%= file_field_tag(:file)%></li>

Thoughts or suggestions.


(Ray Hooker) #2

Looking at video 3, I see that I can modify the route to do as follows:

resources :patients do
post ‘upload’ => 'patients#upload’

This should post to the upload method of patients controller. The question is how to generate the form. If it was simply a button do the upload, I believe could put in the view:

<%= button_to 'UploadFile', upload_patients_path %>

The question is how best to do this with a form? I could do something like:

<%= form_tag("/patients/upload", :method => “put”, :multipart=>true) do %>

  • Upload patient zip file:

  • <%= file_field_tag(:file)%>

  • <%= submit_tag(“Upload”, ‘class’ => “btn btn-success”) %>

<% end %>

But perhaps there is a cleaner way to do this.

(Derek Prior) #3

I would probably make importing from zip a separate controller with its own creat action that handles this case specifically. PatientImportController. Then you can easily see that there’s probably a model (or plain ruby object) you can creat that simply takes the file and does all the work. Your controller becomes really simple.

Hope this helps.

(Ray Hooker) #4

Derek, I think you are right. It definitely gets the philosophy of small simple objects that Sandi Metz promotes and even spoke on our Ruby meetup this past week.