Rails 3 file upload with Carrierwave and Formtastic - ruby-on-rails

I have a Product with many Photos. The relationship is this way:
product.rb
class Product < ActiveRecord::Base
has_many :photos, :dependent => :destroy
attr_accessible :translations_attributes, :active, :category_id, :photos
accepts_nested_attributes_for :translations, :photos
photo.rb
class Photo < ActiveRecord::Base
attr_accessible :photo, :product_id
belongs_to :attachable
mount_uploader :photo, PhotoUploader
and the form looks like this:
form(:html => { :multipart => true }) do |f|
f.inputs "Details" do
f.input :active
f.input :category, :include_blank => false
end
f.inputs "Photos" do
f.input :photos, :as => :file
end
f.buttons
end
The problem is that I get this error when creating/updating the product with a file attached:
undefined method `each' for #<ActionDispatch::Http::UploadedFile:0x007f93c8549828>
I found a question with the same error here, but even trying their solution still have the same problem.
I'm using Friendly_Id, ActiveAdmin, Globalize3, maybe it could have some relationship with them, this is my first project with Rails and right now I don't know what could be...

Your problem is that you're declaring photos as a has_many association in your model, and declaring it as a single file input in your view. Many or one, take your pick :)
What you're probably trying to do is have photos as a nested model in your Product form - something like so:
semantic_form_for #product do |f|
f.semantic_fields_for :photos do |ff|
ff.input :photo, as: :file
To loop over each of the photos in your product, and display a file input field for each.

Related

Activeadmin Formtastic custom input

I have a simple app, which has three models Assessment, Question and AssessmentQuestion
In Assessment i have the association like,
class Assessment < ActiveRecord::Base
has_many :assessment_questions, dependent: :destroy
has_many :questions, through: :assessment_questions
end
In Question i have,
class Question < ActiveRecord::Base
has_many :assessment_questions, dependent: :destroy
has_many :bank_questions, dependent: :destroy
end
In AssessmentQuestion i have,
class AssessmentQuestion < ActiveRecord::Base
belongs_to :question
belongs_to :assessment
end
assessment_questions table has :assessment_id, :question_id and :mark columns
I have admin interface using ActiveAdmin gem.
While creating assessments in admin interface, In admin/assessment.rb i have a form generated by the formtastic gem,
form do |f|
f.inputs do
f.input :name
f.input :duration
f.input :questions, as: :check_boxes, member_label: :id
f.input :creator_id
end
f.actions :commit
end
This looks fine and no problem. The thing that i want is while choosing questions through the checkboxes, i want a textbox below or side to each checkbox containing mark of the questions will be filled in text boxes respectively ( through question.assessment_question.mark association ), so that i can edit the prefilled marks of the questions while creating assessment or leave it as it is.
I have tried, but got some error like
undefined method `to_sym' for {:for=>:questions}:Hash
My code,
form do |f|
f.inputs do
f.input :name
f.input :duration
f.input for: :questions do | question |
f.input :question, as: :select
f.input question.assessment_question.mark
end
f.input :creator_id
end
f.actions :commit
end
Any solutions?
Finally, i figured it out.
I actually created a custom semantic_form_for form _form.html.erb as a partial and included this in admin/assessment.rb file
form partial: 'assessment/form'
That's how i solved.
Reference: http://activeadmin.info/docs/5-forms.html
I would make a custom input that contains a checkbox and a string input:
https://github.com/justinfrench/formtastic#modified--custom-inputs
The solution is obvious, your "f.input :for" misses an "s".
It should be f.inputs

RoR - Searching gem for uploading files

I'm searching a gem (like paperclip or carrierwave) that allows you to upload images. But I want a relationship between the image and the otherside:
I have a table day
And I want a own table for the images so that at the and I'm going to have an has_many belongs_to relationship.
So one day has many images
any suggestions?
If I understand you correctly, you may want to look at the has_many :through relationship for this. We have this working with Paperclip & allows you to have many images to one record:
#app/models/day.rb
Class Day > ActiveRecord::Base
has_many :day_images, :class_name => "DayImage"
has_many :images, :class_name => "Image", :through => :day_images, dependent: :destroy
accepts_nested_attributes_for :day_images, :allow_destroy => true
end
#app/models/day_image.rb
Class DayImage > ActiveRecord::Base
belongs_to :day, :class_name => "Day"
belongs_to :image, :class_name => "Image"
accepts_nested_attributes_for :image, :allow_destroy => true
end
#app/models/image.rb
Class Image > ActiveRecord::Base
has_many :day_images, :class_name => "DayImage"
has_many :days, :class_name => "Day", :through => :day_images, dependent: :destroy
end
The join model would then look like this:
day_images table
id | day_id | image_id | extra attribute | extra attribute | created_at | updated_at
This would allow you to allocate images to the day model using the accepts_nested_attributes_for, like this:
Nested Models
Nested models are pretty tough to get right at first, but get easier the more you do them
Using the models I outlined above, you'll have to add several important factors to make accepts_nested_attributes_for work for you. Here's how:
#app/controllers/days_controller.rb
def new
#day = Day.new
#day.day_images.build.build_image
end
def create
#day = Day.new(day_params)
#day.save
end
private
def day_params
params.require(:day).permit(:day, :variables, day_images_attributes: [:image_id, :extra_attributes, :in, :join, :model, image_attributes: [:image]])
end
This will allow you to create a form like this:
#app/views/days/new.html.erb
<%= form_for #day do |f| %>
<%= f.text_field :day_attribute %>
<%= f.fields_for :day_images do |day_image| %>
<%= day_image.text_field :caption %>
<%= day_image.collection_select(:image_id, Image.where(:user_id => current_user.id), :id, :image_name, include_blank: 'Images') %> --> this will allow you to assign images
<%= day_image.fields_for :image do |i| %>
<%= i.file_field :image %> --> uploads new image
<% end %>
<% end %>
<% end %>
This is all based off live code. If you need any more help, let me know!

rails list all items for a many to many including design

So I have the following models:
Image:
class Image < ActiveRecord::Base
has_many :product_images
has_many :products, :through => :product_images
attr_accessible :asset, :name, :description, :product_ids, :file_content_type, :is_boolean
accepts_nested_attributes_for :product_images
has_attached_file :asset
end
ProductImage:
class ProductImage < ActiveRecord::Base
belongs_to :product
belongs_to :image
attr_accessible :is_thumbnail
end
and Product:
class Product < ActiveRecord::Base
has_many :images, :through => :product_images
has_many :product_images
attr_accessible :name, :description, :thumbnail, :searchTerms, :group_ids, :upload_file_ids
end
Now what I would like to do on the images form is display a checkbox for all the products and then another checkbox for the is_thumbnail attribute
I have had a look into using simple_fields_for but this will only display if the product has already been added. Is there a way to do this?
<%= f.simple_fields_for(:product_images) do |builder| %>
<%= builder.input :is_thumbnail %>
<%= builder.association :products, include_blank: false %>
<% end %>
I'm not very familiar with simple_fields however building form inputs base on an instance will only allow you to "represent" that instance.
This means that you could print all already associated products using
builder.association :products
but if you want to print all products in your database you will need to fetch them, loop and display them in your form.

active_admin and adding multiple images to gallery

I'm using active_admin and carrierwave gems. Have two simple models:
class Image < ActiveRecord::Base
attr_accessible :gallery_id, :file
belongs_to :gallery
mount_uploader :file, FileUploader
end
class Gallery < ActiveRecord::Base
attr_accessible :description, :title, :file, :images_attributes
has_many :images
accepts_nested_attributes_for :images, allow_destroy: true
mount_uploader :file, FileUploader
end
Now my active_admin form for Gallery looks like this:
form do |f|
f.inputs "Gallery" do
f.input :title
end
f.has_many :images do |ff|
ff.input :file
end
f.actions
end
Now I can upload one file, click "Add New Image" and upload another one. Instead of it, I'd like to click "Add new Image", select multiple files and upload them all at once. Any idea how can I implement it?
For a Gallery form with multiple image uploads you can try this
admin/galleries.rb
form do |f|
f.inputs "Gallery" do
f.input :name
end
f.has_many :images do |ff|
ff.input :file
end
end
In model/gallery.rb:
attr_accessible :images_attributes
In model/gallery.rb (add after relations):
accepts_nested_attributes_for :images, :allow_destroy => true

Assigning a nested attribute with Formtastic

I've been trying to figure this one out for a while but still no luck. I have a company_relationships table that joins Companies and People, storing an extra field to describe the nature of the relationship called 'corp_credit_id'. I can get the forms working fine to add company_relationships for a Person, but I can't seem to figure out how to set that modifier field when doing so. Any ideas?
More about my project: People have many companies through company_relationships. With that extra field in there I am using it to group all of the specific relationships together. So I can group a person's Doctors, Contractors, etc.
My models:
Company.rb (abridged)
class Company < ActiveRecord::Base
include ApplicationHelper
has_many :company_relationships
has_many :people, :through => :company_relationships
Person.rb (abridged)
class Person < ActiveRecord::Base
include ApplicationHelper
has_many :company_relationships
has_many :companies, :through => :company_relationships
accepts_nested_attributes_for :company_relationships
company_relationship.rb
class CompanyRelationship < ActiveRecord::Base
attr_accessible :company_id, :person_id, :corp_credits_id
belongs_to :company
belongs_to :person
belongs_to :corp_credits
end
My form partial, using formtastic.
<% semantic_form_for #person do |f| %>
<%= f.error_messages %>
<% f.inputs do %>
...
<%= f.input :companies, :as => :check_boxes, :label => "Favorite Coffee Shops", :label_method => :name, :collection => Company.find(:all, :conditions => {:coffee_shop => 't'}, :order => "name ASC"), :required => false %>
So what I would like to do is something like :corp_credit_id => '1' in that input to assign that attribute for Coffee Shop. But formtastic doesn't appear to allow this assignment to happen.
Any ideas on how to do this?
Are you looking for something like
<% semantic_form_for #person do |form| %>
<% form.semantic_fields_for :company_relationships do |cr_f| %>
<%= cr_f.input :corp_credit_id %>
<% end %>
It is in the documentation

Resources