I have a column of product views in a database (e.g. top, bottom, front, back). I’m trying to generate a series of file inputs to allow the user to upload an image for each view. This is the result I’m after:
...
<label>Top</label>
<input type="file" name="image[Top]"><br>
<label>Bottom</label>
<input type="file" name="image[Bottom]"><br>
<label>Front</label>
<input type="file" name="image[Front']"><br>
...
This is what I’m trying:
<%= views = View.order('name ASC').all.map { |view| [view.name, view.id] } %>
<%= views.each { |view| label(view); file_field('image', view) } %>
However, all this does is print out the views array a couple of times. Hopefully you Rails experts can point me in the right direction. (I apologize in advance if I’m butchering Ruby.)
I’d suggest to move your logic out of the view into controller or helper method . Your views_controller.rb :
This gives you an array of instance objects , which means you can access the view name simply by :
Now , in your view (which should be a form_for or form_tag , so you can choose images and upload them ), you can :