SORU
28 Ocak 2014, Salı


4 çoklu resim raylar veya upload carrierwave kullanarak dosya

Nasıl "ctrl çeşit çeşit anahtar" aynı zamanda... post_controller ve post_attachments modeli var. seçerek birden fazla resim yüklemek için nasıl yapabilirim?

Bana örnek verebilir misiniz?

Herhangi bir basit bir yolu var mı?

CEVAP
28 Ocak 2014, Salı


Bu sıfırdan birden fazla resim raylar carrierwave kullanarak 4 yüklemek için çözümdür

Ya da çalışma demo bulabilirsiniz : Multiple Attachment Rails 4

Bu adımları sadece.

rails new multiple_image_upload_carrierwave

Gem dosyası

gem 'carrierwave'
bundle install
rails generate uploader Avatar 

Sonrası iskele oluşturun

rails generate scaffold post title:string

Post_attachment iskele oluşturun

rails generate scaffold post_attachment post_id:integer avatar:string

rake db:migrate

Sonrası.rb

class Post < ActiveRecord::Base
   has_many :post_attachments
   accepts_nested_attributes_for :post_attachments
end

Post_attachment.rb

class PostAttachment < ActiveRecord::Base
   mount_uploader :avatar, AvatarUploader
   belongs_to :post
end

Post_controller.rb

def show
   @post_attachments = @post.post_attachments.all
end

def new
   @post = Post.new
   @post_attachment = @post.post_attachments.build
end

def create
   @post = Post.new(post_params)

   respond_to do |format|
     if @post.save
       params[:post_attachments]['avatar'].each do |a|
          @post_attachment = @post.post_attachments.create!(:avatar => a)
       end
       format.html { redirect_to @post, notice: 'Post was successfully created.' }
     else
       format.html { render action: 'new' }
     end
   end
 end

 private
   def post_params
      params.require(:post).permit(:title, post_attachments_attributes: [:id, :post_id, :avatar])
   end

/Mesaj görüntüleme/_form.html.erb

<%= form_for(@post, :html => { :multipart => true }) do |f| %>
   <div class="field">
     <%= f.label :title %><br>
     <%= f.text_field :title %>
   </div>

   <%= f.fields_for :post_attachments do |p| %>
     <div class="field">
       <%= p.label :avatar %><br>
       <%= p.file_field :avatar, :multiple => true, name: "post_attachments[avatar][]" %>
     </div>
   <% end %>

   <div class="actions">
     <%= f.submit %>
   </div>
<% end %>

Herhangi bir mesaj Eki ek ve bir liste düzenlemek için. /Posts/show görünümlerde.html.erb

<p id="notice"><%= notice %></p>

<p>
  <strong>Title:</strong>
  <%= @post.title %>
</p>

<% @post_attachments.each do |p| %>
  <%= image_tag p.avatar_url %>
  <%= link_to "Edit Attachment", edit_post_attachment_path(p) %>
<% end %>

<%= link_to 'Edit', edit_post_path(@post) %> |
<%= link_to 'Back', posts_path %>

Form Eki düzenlemek için güncellemegörüş/post_attachments/_form.html.erb

<%= image_tag @post_attachment.avatar %>
<%= form_for(@post_attachment) do |f| %>
  <div class="field">
    <%= f.label :avatar %><br>
    <%= f.file_field :avatar %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

Update yöntemini değiştirmekpost_attachment_controller.rb

def update
  respond_to do |format|
    if @post_attachment.update(post_attachment_params)
      format.html { redirect_to @post_attachment.post, notice: 'Post attachment was successfully updated.' }
    end 
  end
end

Raylar 3 Her iki model ve accept_nested_attribute içinde attribute_accessible öznitelik erişilebilir kaldırılmış olduğundan model sonrası için tanımlayabilirsiniz güçlü parametrelerini tanımlamak için gerek ve 4 raylar.

Bir ek düzenleme için biz bir anda tüm ekleri cant değiştirin. bir tarafından ek bir değiştireceğiz, ya da senin kurala göre değiştirebilirsiniz, ben sadece herhangi bir ek güncellemek için nasıl göstermek.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Michael Zhang

    Michael Zhan

    8 EYLÜL 2012
  • Makeup Lover

    Makeup Lover

    12 HAZİRAN 2011
  • superemposed

    superemposed

    25 Aralık 2007