0

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.)

1
  • I don't have a suggestion, but the first line in your try shouldn't have <%=, change it to <% views = ... Commented Nov 20, 2012 at 22:47

1 Answer 1

2

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 %>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.