понедельник, 12 сентября 2011 г.

Re: Галерея изображений из вложенных полей

Попробую кодом объяснить свою идею

post.rb

class Post < ActiveRecord::Base
  has_many :attachments, :as => :attachmentable, :dependent => :destroy
  accepts_nested_attributes_for :attachments, :allow_destroy => true,
  :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }


attachment.rb

class Attachment < ActiveRecord::Base
  belongs_to :post


posts_controller.rb

  def new
    @post = Post.new
    2.times do
      attachment = @post.attachments.build
    end

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @post }
    end
  end


posts/edit.html.haml

= simple_form for @post
  = f.input :title
  = f.input :body
  = f.simple_fields_for :attachments do |builder|
    = render 'attachments/form', :f => builder



attacments/_form.html.haml

    # 1 экземпляр
    =image_tag (?attachment).image_url(:preview) if (?attachment).image?    # здесь надо вывести значение поля :image, как привью изображения
    = f.input :image
    = f.input :_destroy
    # 2 экземпляр
    =image_tag (?attachment).image_url(:preview) if (?attachment).image?    # здесь надо вывести значение поля :image, как привью изображения
    = f.input :image
    = f.input :_destroy



posts/show.html.haml

- @post.attachments.each do |attachment|
    =image_tag attachment.image_url(:preview) if attachment.image?

--
--
Данное сообщение отправлено Вам, так как Вы являетесь подписчиком группы "RubyOnRails to russian" на группах Google.
FAQ группы находится по адресу: http://ru.wikibooks.org/wiki/RubyFAQ
 
Для того, чтобы отправить сообщение в эту группу, пошлите его по адресу
ror2ru@googlegroups.com
Чтобы отменить подписку на эту группу, отправьте сообщение по адресу: ror2ru-unsubscribe@googlegroups.com
Дополнительные варианты находятся на странице группы http://groups.google.com/group/ror2ru?hl=ru

Комментариев нет:

Отправить комментарий