i'm having trouble can't display images options select form. want create photo album , @ same time select photos add album.
i tried "collection_check_boxes" can't existing images display options. , haven't tried create (save) yet.
i have associations. i'm using paperclip gem image.
models:
class album < activerecord::base belongs_to :user has_many :photos end class photo < activerecord::base belongs_to :owner, class_name: 'user' belongs_to :album has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png" validates_attachment_content_type :image, content_type: /\aimage\/.*\z/ end
controller:
class albumscontroller < applicationcontroller before_action :authenticate_user!, only: [:create, :new] def index @albums = current_user.albums end def new @photos = current_user.photos.all @album = album.new end def create @album = albums.new(album_params) if @album.save redirect_to user_album_path(@album) else redirect_to root_path end end def show @album = album.find(params[:id]) end def add_photo end private def album_params params.require(:album).permit(:name) end end
views:
<%= form_for @album, url: user_albums_path |f| %> <form> <div class="field"> <%= f.label :name %><br /> <%= f.text_field :name %> </div> ####################tried didn't work because image isn't text_method <div> <%= collection_check_boxes(:albums, :photo_ids, current_user.photos.all, :id, image_went_here) %> </div> #################### <div class="actions"> <%= f.submit "create" %> </div> </form> <% end %>
collection_check_boxes(:albums, :photo_ids, current_user.photos.all, :id, :id) |b| b.label(class: "check_box") { b.check_box(class: "check_box") + image_tag(object.image.url) } end
where object
given block representing photo
in current_user.photos.all
. may need change object.photo.url
reference match photo
model. see examples of giving collection_check_boxes
block in documentation.
Comments
Post a Comment