Reputation: 10219
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.)
Upvotes: 0
Views: 161
Reputation: 4315
I'd suggest to move your logic out of the view into controller or helper method . Your views_controller.rb :
@views = View.all.order('name ASC')
This gives you an array of instance objects , which means you can access the view name simply by :
@views.each do |v|
var = v.name
# other code for iterating on View ...
end
Now , in your view (which should be a form_for or form_tag , so you can choose images and upload them ), you can :
<%= form_for(@catalogue) do |f| %>
<%= @views.each do |v| %>
<%= f.label v.name %> < br/ >
<%= f.file_field v.name %>
<% end %>
<% f.submit %>
<% end %>
Upvotes: 2