nested form is not displayed - ruby-on-rails

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 }

Related

Rails: File to Upload does not get passed from Form to the controller

This is the Form. All of the fields get passed (and saved) except the one containing the File.
I have checked that using the
render plain: params[:article].inspect method
giving out this (I have entered the value "n" for all fields):
{"country"=>"n", "region"=>"n", "town"=>"n", "street"=>"n", "company"=>"n", "title"=>"n", "content"=>"n"}
I am leaving out superfluous fields here to make the Form shorter:
<%= form_for(#article, html: { multipart: true }) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :country %>
<%= f.text_field :country, :required => true,
placeholder: "enter country" %>
</div>
<%= f.label :content %>
<%= f.text_field :content, :required => true, placeholder: "town..." %>
</div>
</div>
</div>
</div>
<span class="picture">
<%= form_for #article, html: { multipart: true } do |f| %>
<%= f.text_field :title %>
<%= fields_for :pictures do |ff| %>
<%= ff.file_field :picture %>
<% end %>
<% end %>
</div>
I have also tried the slight variation here, but no change
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for
The create method at the Controller is like this:
def create
#article = current_user.articles.build(article_params)
if #article.save
flash[:success] = "Article created!"
redirect_to root_url
else
render 'articles/new'
end
end
and yes, the new method in the Articles controller, is like I was indicated by peers here:
def new
#article = current_user.articles.build
#article.pictures.build
end
The Article Model
class Article < ActiveRecord::Base
belongs_to :user
has_many :pictures
accepts_nested_attributes_for :pictures, allow_destroy: true
And the pictures Model
class Picture < ActiveRecord::Base
belongs_to :article
mount_uploader :picture, PictureUploader
end
Change your <%= fields_for :pictures do |ff| %> to <%= f.fields_for :pictures do |ff| %>

How to add multiple images using carrierwave

i created form, but on click ,choose image option is not appending to add multiple images.
Please give me a solution for this
my property form.html.erb
<%= simple_nested_form_for ([:generic_users, #property]) do |f| %>
<%= f.input :image, required: true %>
<p><%= image_tag #property.image_url(:thumbnail) if #property.image? %></p>
<%= f.hidden_field :image_cache %>
<%= f.simple_fields_for :property_images do |i| %>
<%= i.input :image %>
<p><%= image_tag i.object.image_url(:thumbnail) if i.object.image? %></p>
<%= i.hidden_field :image_cache %>
<%= i.link_to_remove 'Remove',method: :delete, class: 'btn btn-danger'%>
<% end %>
</div>
</div>
<hr>
<%= f.link_to_add 'Add Image', :property_images, class: 'btn btn-primary', data: {target: '.additional-images'} %>
<hr>
<% end %>
this is my propertyImage model
class PropertyImage < ActiveRecord::Base
belongs_to :property
validates_presence_of :image
mount_uploader :image, ImageUploader
end
my property.rb
class Property < ActiveRecord::Base
has_many :property_images, dependent: :destroy
accepts_nested_attributes_for :property_images, allow_destroy: true
end
This is how i am getting page , on clicking Add Image , multiple option for choose image is not appearing.
Please give me a solution
It's not clear to me from your code where the actual file input is being rendered. However for multiple file selections you would want code something like this:
<%= f.file_field :image, multiple: true, require: true %>
You can use
<%= file_field_tag "images[]", type: :file, multiple: true %>
instead of <%= f.file_field :image, multiple: true, require: true %>
if you will upload multiple images then there will be an array of images will be create in the from parameter with name images[]

Rails: Multiple image upload at the same time with Dragonfly

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 %>

rails accept nested atributes for a polymorphic association

I have an error "Can't mass-assign protected attributes: Upload", but I have assigned it to be accessible.
This is a nested form with a polymorphic association.
Models
class Upload < ActiveRecord::Base
attr_accessible :link, :post_id
belongs_to :uploadable, polymorphic: true
end
class Post < ActiveRecord::Base
attr_accessible :description, :title, :uploads_attributes
has_many :uploads, as: :uploadable
accepts_nested_attributes_for :uploads, :reject_if => lambda { |a| a[:content].blank?
}, :allow_destroy => true
end
I tried too put accept_nested ... for :uploadable but tells me dont exist the association
The action new on the controller is this one
def new
#post = Post.new
#post.uploads.new
end
and here is the form for create
<%= form_for [:admin,#post], remote: true, :html => {:multipart => true} do |f| %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title%>
</div>
<div class="field">
<%= f.label :description%><br />
<%= f.text_area :description %>
</div>
<div>
<%= f.fields_for :upload do |builder| %>
<%= render 'upload_fields', f: builder %>
<% end %>
<%= link_to_add_fields "Add Upload", f, :uploads %>
</div>
<div class="actions">
<%= f.submit%>
</div>
<% end %>
The partial ...
<fieldset>
<%= f.label :file %><br />
<%= f.file_field :file %>
<%= f.hidden_field :_destroy %>
<%= link_to "remove", '#', class: "remove_fields" %>
</fieldset>
Dont think the javascript affects, so Im not going to put it here.
How I cna solve "Can't mass-assign protected attributes" on polymorphic asociations ?
Plz need help on this anyone. Cant belive I cant upload files, looks so simple on tutorials, and Its not working, or I get a Can't mass assign orthe upload its not saved ....
Try to use #post.uploads.build instead of #post.uploads.new
The associated model needs to know the identity of her parent to save the relationship.
I recommend you the following railscast: Polymorphic Association.
#uploads_controller.rb
before_filter :load_uploadable
def create
#upload = #uploadable.uploads.new(params[:upload])
....
end
private
def load_uploadable
resource, id = request.path.split('/')[1, 2] # /posts/1
#uploadable = resource.singularize.classify.constantize.find(id)
end
This line inside your view:
<%= f.fields_for :upload do |builder| %>
Should be this:
<%= f.fields_for :uploadable do |builder| %>
Because the association on the Post model is called "uploadable", not "upload".
For nested attributes to work, you will need to specify the model does accept nested attributes for this model, which can be done by putting this line underneath the belongs_to in your model:
accepts_nested_attributes_for :uploadable
And then you will need to make these attributes accessible, which you can do with this:
attr_accessible :uploadable_attributes

nested form display error

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 :image, presence: true
And here's how the kindergartens controller looks like:
def new
#kindergarten = Kindergarten.new
#kindergarden.photos.build
end
Now, when #kindergarten new is generated i get the following error:
undefined method 'photos' for nil:NilClass
Application Trace | Framework Trace | Full Trace
app/controllers/kindergartens_controller.rb:5:in `new'
You've written #kindergarden.photos.build instead of #kindergarten.photos.build. I hope the typo is not in the actual code.
Also try #kindergarten=Kindergarten.create . If you are calling new just creates an unsaved record, which should be followed by a call to the save method. That could be the reason for the NilClass error.

Resources