ruby on rails - How do you find what place in an array something is? -


user edit view:

<%= form_for(@user, :html => {:multipart => true}) |f| %>     <%= f.text_field :name, placeholder: :name %>     <%= f.submit "save" %>      <%= f.fields_for :photos |photo_fields| %>         <%= image_tag(photo_fields.object.photo.url(:user_thumbnail) %>         <%= f.radio_button :avatar, @selected_photo_number %> 

so show photos uploaded user , radio button next each of them. how can made when click first 1 @selected = 0 , second 1 @selected = 1 , on. need know in place photo in array @user.photos[]. thanks!

instead of @selected_photo_number need photo_fields.object.place in array??

i think you're looking index.

>> array = [1,2,3,4,5] >> array.index(1) # 0 >> array.index(3) # 2 

but code, think you're looking each_with_index instead

<% f.object.photos.each_with_index |photo, index| %>   <%= f.fields_for :photos, [photo] |photo_fields| %>     <%= image_tag(photo_fields.object.photo.url(:user_thumbnail) %>     <%= f.radio_button :avatar, index %> 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -