I'm using Dragonfly to upload images to model Photo which is associeted to others models. For example:
MODEL: Post
class Post < ActiveRecord::Base
has_many :photos, dependent: :destroy
accepts_nested_attributes_for :photos, allow_destroy: true,
reject_if: lambda {|attributes| attributes['image'].blank?}
MODEL Photo
class Photo < ActiveRecord::Base
extend Dragonfly::Model::Validations
dragonfly_accessor :image
dragonfly_accessor :image do
after_assign { |img| img.encode!('jpg', '-quality 80') }
end
It works fine, even when I change the input to upload multiple files at the same time
FORM VIEW:
works:
<%= f.fields_for :photos do |photo| %>
<%= photo.file_field :image, class: "form-control" %>
<% end%>
doesn't works:
<%= f.fields_for :photos do |photo| %>
<%= photo.file_field :image, class: "form-control", multiple: true, name: 'photo[image]' %>
<% end%>
How can I upload multiple files at the same time to associeted model?
Thanks!
UPDATE:
I tried:
controller. Create action
#post = Post.new(seminovo_params)
params[:photos]['image'].each do |a|
#photo = #post.photos.create!(:image => a)
end
View
<%= f.fields_for :photos, #post.photos.new do |photo| %>
<%= photo.file_field :image, :multiple => true, name: "photos[image][]" %>
Yo need to define params and get those params this way
Your new method should contain
def new
# Your new form object
#photo = #post.photos.build
end
In controller method use this
params[:photos]['avatar'].each do |a|
#photo = #post.photos.create!(:image => a)
end
Your form should look like,
<%= form_for(#post, :html => { :multipart => true }) do |f| %>
<%= f.fields_for :photos do |p| %>
<div class="field">
<%= p.label :image %><br>
<%= p.file_field :image, :multiple => true, name: "photos[image][]" %>
</div>
<% end %>
Related
In my models I have
class Blog < ActiveRecord::Base
has_many :tags, :dependent => :destroy
accepts_nested_attributes_for :tags, :allow_destroy => true
end
class Tag < ActiveRecord::Base
belongs_to :blog
validates :blog, :name, presence: true
end
Blog Controller
def new
#blog = Blog.new
#blog.tags.build
end
_form.html.erb
<%= form_for #blog, html: { multipart: true } do |f| %>
<div class="form-group">
<%= f.text_field :title, placeholder: 'Title', class: ('form-control') %>
</div><br>
<%= f.fields_for :tags do |builder| %>
<div class="form-group">
<%= builder.text_field :name, placeholder: 'Tags' %>
</div><br>
<% end %>
<div class="actions text-center">
<%= f.submit 'Submit', class: 'btn btn-primary' %>
</div>
<% end %>
Blog Controller
def create
#blog = Blog.new(blog_params)
binding.pry
end
def blog_params
params.require(:blog).permit(:title, :author, :text, :avatar, :banner, :tags_attributes => [:id, :name])
end
At my binding, it says #blog's error message is that it can't be saved because the Tag object is missing a blog_id. I have looked everywhere and I have tried to replicate my code to match other solutions but to no success.
If it helps, in my params when I submit the form I get this
"tags_attributes"=>{"0"=>{"name"=>"dsfsf"}}
that's because your #blog is not persisted in the db yet, so you won't have the id.
In your Tag model, remove :id from validation.
You should be able to just do Blog.create(blog_params)
Rails should handle the rest for you.
i'm trying to save two object (image and event) using one form (event form),
but the when i submit the button there is only event object that created, the images object not save to images table,
models/event.rb
has_one :images, :class_name => '::Refinery::Image'
accepts_nested_attributes_for :images, :allow_destroy => true
models/image.rb
belongs_to :events, class_name: 'Refinery::Events::Event'
events_controller.rb
def new
#event = Event.new
#event.build_images
end
def create
#event = Event.new(event_params)
end
def event_params
params.require(:event).permit(:nama, :deskripsi, images_attributes:[:id, :image_name, :image_size, :image_width, :image_height, :created_at, :updated_at, :image_mime_type, :image_uid, :image_title, :image_alt, :event_id] )
end
events/_form.html.erb
<%= form_for [refinery, :events, #event], :html => { :multipart => true } do |f| %>
<%= render '/refinery/admin/error_messages',
:object => #event,
:include_object_name => true %>
<div class='field nama_field string_field'>
<%= f.label :nama %>
<%= f.text_field :nama %>
</div>
<div class='field deskripsi_field text_field'>
<%= f.label :deskripsi %>
<%= f.text_area :deskripsi, :rows => 8 %>
</div>
<%= f.fields_for :images do |ff| %>
<div>
<%= ff.label :images %>
<%= ff.file_field :images %>
</div>
<% end %>
<div class='actions'>
<%= f.submit t('.send') %>
</div>
<% end %>
anyone can help me why the images not saved into images table?
I have the following form declaration for a new kindergarten
<%= form_for #kindergarten, :html => {:multipart => true} do |f|%>
<%= render 'shared/error_messages', object: f.object %>
</br>
<%= f.fields_for :photos do |p| %>
<%= p.label 'upload photo'%>
<%= p.file_field :image %>
<% end %>
</br>
<%= render 'about_company', f: f%>
</br>
<%= render 'contact', f: f %>
<%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
<%end%>
The logic behind this is that 1 kindergarten can have multiple photos.
Here are the model declarations:
Kindergarten
has_many :photos, limit: 7, dependent: :destroy
accepts_nested_attributes_for :photos
Photo
attr_accessible :image, :logo, :kindergarten_id
belongs_to :kindergarten
mount_uploader :image, ImageUploader
validates :kindergarten_id, presence: true
validates :photo, presence: true
I am struggling with this for a few hours now, and I can't understand why the "upload file" button and label are not showing up. When i say now showing up, i mean they are completely ignored and not present in the rendered html.
Before rendering the form for a new # kindergarden, you'll need to build a photo association for this to work:
def new
#kindergarden = Kindergarden.new
#kindergarden.photos.build # provides a photo object to use in the form
# rest of your action
end
Also, if you wanted multiple photo fields in the form, you could do something like:
5.times { #kindergarden.photos.build }
lets say I have 2 models like News, Clients.
Using paperclip's default options, I need to create for each of them additional columns like (photo_file_name .....)
but I just want to create different model, let's say Asset
asset.rb
belongs_to :client
has_attached_file :photo, :styles => {:small => "300x300>"}
client.rb
has_one :asset, :dependent => :destroy
accepts_nested_attributes_for :asset
clients_controller.rb
def new
#client = Client.new
#client.build_asset
end
_form.html.erb
<%= form_for #client, :html => {:multipart => true} do |f| %>
<%= f.fields_for :asset do |asset| %>
<%= asset.label :photo %><br/>
<%= asset.file_field :photo %>
<% end %>
<% end %>
For now this is working, but how to show it in show view ? i'm doing this:
<%= image_tag #client.url(:small) %>
I know this is not correct, because #client.asset does not have url column,
how to do it ?
Just like Mikhail Nikalyukin said, you should call
<%= image_tag #client.photo.url(:small) %>
instead of
<%= image_tag #client.url(:small) %>
UPDATE
If i remove the reject_if from this line :
accepts_nested_attributes_for :image_blogs, :reject_if => lambda { |t| t['image_blog'].nil? }
It works, how can it be modified to work as intended and prevent an image creation when nil?
I am using the following tutorial to create Post with images :
http://sleekd.com/general/adding-multiple-images-to-a-rails-model-with-paperclip/
The goal is to have a post element containing 0-n images blog. images blog is a model containing a paperclip. I am trying to have image_blog elements created at the same time than the post. To do so I use nested forms.
class PostsController < ApplicationController
def new
#post = Post.new
3.times{ #post.image_blogs.build}
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #post }
end
end
end
-
class ImageBlog < ActiveRecord::Base
belongs_to :post
attr_accessible :image
has_attached_file :image , :styles => { :small => "150x150>", :large => "320x240>" }
end
-
class Post < ActiveRecord::Base
has_many :image_blogs, :dependent => :destroy
validates :title, :content, :presence => true
validates :title, :uniqueness => true
acts_as_taggable
has_attached_file :image
accepts_nested_attributes_for :image_blogs, :reject_if => lambda { |t| t['image_blog'].nil? }
end
-
<%= form_for(#post,:html => { :multipart => true }) do |f| %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :content %><br />
<%= f.text_area :content, :class => "mceEditor" %>
</div>
<%= f.fields_for :image_blogs, do |ib|%>
<p>
<%= ib.label "Image du post"%>
<%= ib.file_field :image %>
<%#= ib.check_box :_destroy%>
<%#= ib.label :_destroy,"Effacer l'image" %>
</p>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I checked that everything was correct using different tutorials about nested paperclip, but it still does not work. The post is created but the images are not copied and no imageblog elements are created.
Why is there no error? why is it not working?
I believe you have to make a slight correction on the accepts_nested_attributes_for, inside the lambda, the object you are evaluating is image (attribute of ImageBlog) instead of image_blog, this way:
accepts_nested_attributes_for :image_blogs, :reject_if => lambda { |t| t['image'].nil? }