I'm creating a basic application where user has a profile and can upload upto 5 pictures of him/her.I created user profile page, I would want to allow the user to upload pictures in different page. Like having a link Add/Edit Photo and on clicking it to take to a different page and upon submit should redirect back to profile page and update/insert records. So I'm kind of confused should I do this under photos model/controller or member model/controller.Here is my sample code and link is below
I'm using paperclip for image upload
Member view
<%= link_to 'Add/Edit Photo' , edit_member_path(current_member.id) %>
<%= form_for(#member, :remote=> true, html: {
:multipart => true,
class:"form-light padding-15"}) do |f| %>
<%= f.label :firstname %>
<%= f.text_field :firstname, autofocus: true, :class => "form-control",:placeholder =>"FirstName" %>
<%= f.label :lastname %>
<%= f.text_field :lastname,autofocus: true, :class => "form-control",:placeholder =>"LastName"%>
<% end %>
class Member < ActiveRecord::Base
attr_accessible :firstname, :lastname, :user_id, :dob,:gender,:l_country_id,:age,
:l_state_id,:l_city,:g_country_id,:residency_status,:marital_status,
:community,:sub_community,:height,:weight,:complexion,:body_type,
:diet,:smoke,:drink,:education,:working_as,:working_with,:mobile_no,:about_yourself,:disability,:photos_attributes
has_many :photos
belongs_to :country
belongs_to :user
belongs_to :state
accepts_nested_attributes_for :photos
end
class Photo < ActiveRecord::Base
belongs_to :member
has_attached_file :data, :styles => { :thumb => "100x100#",
:medium => "500x500#",
:large => "700x700>" },
:url => "/assets/member/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/member/:id/:style/:basename.:extension"
#validates_attachment_presence :data
validates_attachment_content_type :data, :content_type => /\Aimage/
#validates_attachment_size :data, :less_than => 5.megabytes
validates_attachment_content_type :data, :content_type => ['image/jpeg', 'image/png']
attr_accessible :member_id,:data
end
As the separate pages are for photos only, my suggestion is to use PhotosController. And you can use nested resources to get the member_id from the url, if that is your concern.
http://guides.rubyonrails.org/routing.html#nested-resources
Related
I'm trying to upload images using nested attributes, but I'm getting this error:
Rack::Utils::ParameterTypeError - expected Array (got Rack::Utils::KeySpaceConstrainedParams) for param `project_images_attributes':
Whenever I edit my project to add new images. I thought if I added child_index: ProjectImage.new.object_id, it'll work
<%= f.simple_fields_for :photos, ProjectImage.new, child_index: ProjectImage.new.object_id do |ff| %>
<%= ff.label :photo, "Upload Photo" %>
<%= ff.file_field :photo, multiple: true, name: "project[project_images_attributes][][photo]" %>
<% end %>
Edit:
ProjectImage Model
class ProjectImage < ActiveRecord::Base
belongs_to :project
paperclip_opts = {
:styles => { :large => "800x800>", :medium => "x430>", :frontpage_thumb => "130x95#", :thumb => "150x150#" },
:convert_options => { :all => "-quality 75 -strip" }
}
has_attached_file :photo, paperclip_opts
validates_attachment_content_type :photo, content_type: /\Aimage\/.*\Z/
end
Project Model
class Project < ActiveRecord::Base
has_many :project_images, dependent: :destroy
accepts_nested_attributes_for :project_images, allow_destroy: true
end
You need to change this
<%= f.simple_fields_for :photos, ProjectImage.new, child_index: ProjectImage.new.object_id do |ff| %>
to
<%= f.simple_fields_for :project_images, ProjectImage.new, child_index: ProjectImage.new.object_id do |ff| %>
This question already has answers here:
How to make a field required in Rails?
(2 answers)
Closed 7 years ago.
I'm trying to make a file_field required before the form is able to be submitted, using Rails and ERB, and I'm drawing a blank.
<%= form_for Image.new, html: {multipart: true} do |i| %>
<%= i.label :description %>
<%= i.text_field :description %>
<%= i.file_field :image %> <------**** This Line ****
<%= i.submit "Upload" %>
<% end %>
I've tried using 'required' in various ways and searched the web but to no avail. Is this something that I should seek to validate in the model instead?
My model is this:
class Image < ActiveRecord::Base
has_many :comments, dependent: :destroy
has_many :likes, dependent: :destroy
belongs_to :user
has_attached_file :image, :styles => { :large => "600x600>", :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
end
At the bottom of the list of suggested answers I found what I was looking for in this stack thread
validates :image, :presence => true
I simply had to add that to my model and it doesn't let the form submit without an image attachment.
I wan't to add image upload field inside active admin interface.This is the view where I want to be able get photo upload
I tried some earlier suggestions From here
ActiveAdmin.register Product do
form :html => { :multipart=>true } do |f|
f.inputs :new_product do
f.input :name
f.input :price
f.input :category
f.input :description
f.has_many :prod_images do |p|
p.input :photo, :as => :file, :label => "Image",:hint => p.template.image_tag(p.object.photo.url(:thumb))
p.input :_destroy, :as=>:boolean, :required => false, :label => 'Remove image'
end
end
f.buttons
end
Using this example I got such error
undefined method `klass' for nil:NilClass
It says that error is from here app/views/active_admin/resource/new.html.arb where line #1 raised
but how can I access that file, because in explorer it is not showing up?
Thanks
Try building a prod image, like this.
f.has_many :prod_images, f.object.prod_images.build do |p|
I managed to get file upload field with this code
ActiveAdmin.register Product do
form :html => { :enctype => "multipart/form-data" } do |f|
f.input :photo, :as => :file, :hint => f.template.image_tag(f.object.photo.url(:thumb))
end
But now I can't add submit buttons :D So I am still working on it :)
EDIT
ActiveAdmin.register Product do
form :html => { :enctype => "multipart/form-data" } do |f|
f.input :photo, :as => :file
f.buttons
end
end
This just shows buttons like Create and Cancel, but not showing up file field, I checked formastic examples, but without success.
EDIT2
class Product < ActiveRecord::Base
attr_accessible :category_id, :description, :manufacturer_id, :name, :photo
extend FriendlyId
has_attached_file :photo,
:styles => {
:thumb=> "100x100#",
:large => "290x170",
:medium=> "120x120"}
friendly_id :name, use: [:slugged, :history]
belongs_to :manufacturer
belongs_to :category
end
I'm building a real-estate webapp, and every Ad should have an unlimited number of pictures (Asset).
In the Ad#new form I want to give the users the option to upload as many picture as they want.
I've created an Ad model which has_many Assets. An Asset is a model that holds a paperclip for a picture.
This is the code:
class Asset < ActiveRecord::Base
attr_accessible :picture
has_attached_file :picture, :styles => { :large => "600x600", :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
belongs_to :ad
end
class Ad < ActiveRecord::Base
attr_accessible :contact_cell, :description, :price, :title, :user_id
belongs_to :user
has_many :assets, dependent: :destroy
end
How should the form_for look like if I want to give the user the option to add unlimited # of photos before he/she submits the form?
If you want to do it with html5, you can easily do it by doing something like (the main important thing to notice, is that on html5 you can add the attribute multiple=true, that will create a multiple upload for you):
<%= simple_form_for(#ad, html: { multipart: true }) do |f| %>
<%= f.input :contact_cell %>
<%= f.input :description %>
<%= f.input :price %>
<%= f.input :title %>
<%= file_field_tag('ad_assets_picture', multiple: true, name: "ad[assets_attributes][][picture]") %>
<%= f.button :submit %>
<%- end %>
otherwise you can just follow the nested form example, there is a nice webcast about it
I have been developing a rails app that uploads and processes images. Images, along with other string information is submitted via a form_for. I've been researching this topic for about 16 hours now and no solution has worked. Honestly it's like rails isn't even reading my code.
One Processmodel has many Assets, where an Asset is just a model to hold one image file. When creating processmodels, I can never access the asset, always recieving the cannot mass-assign attirbutes: assets_attributes
Completed 500 Internal Server Error in 13ms
ActiveModel::MassAssignmentSecurity::Error (Can't mass-assign protected attributes: asset):
app/controllers/process_controller.rb:20:in `new'
app/controllers/process_controller.rb:20:in `create'
-
This form is used in new.html.erb
<%= semantic_form_for #processmodel, :url => { :action => 'create' }, :html => { :multipart => true } do |f| %>
<%= f.input :batch, :as => :string, :name => "Batch" %>
<%= f.input :batchset, :as => :string, :name => "Batchset" %>
<%= f.input :numSlots, :as => :number, :name => "Number of slots" %>
<%= f.input :key, :as => :file, :name => "Key" %>
<%= f.semantic_fields_for :asset do |asset| %>
<%= asset.input :asset, :as => :file, :label => "Image" %>
<% end %><br />
<%= f.submit %>
<% end %>
-
class Processmodel < ActiveRecord::Base
attr_accessible :user_id, :batch,
:batchset, :numSlots,
:key,:assets_attributes
attr_accessor :key_file_name
has_many :assets, :dependent => :destroy
belongs_to :user
has_attached_file :key
# :url => Rails.root.join('/assets/readimages/:basename.:extension'),
# :path => Rails.root.join('/assets/readimages/:basename.:extension'),
accepts_nested_attributes_for :assets, :allow_destroy => true
.
.
.
end
-
require 'RMagick'
class Asset < ActiveRecord::Base
attr_accessible :results_string,
:name,
:ambiguous_results,
:image
belongs_to :batch_element
belongs_to :processmodel
has_attached_file :image
validates_attachment_presence :image
end
-
class ProcessController < ApplicationController
def create
#Processmodel = Processmodel.new(params[:processmodel])
#Processmodel.save
all_img = Array.new(#processmodel.assets.all)
respond_to do |format|
if #processmodel.beginRead(...)
redirect_to :action => 'results_main', :controller => 'results'
else
format.html { render action: "new" }
end
end
end
-
def new
#processmodel = Processmodel.new
#5.times{#processmodel.assets.build}
respond_to do |format|
format.html #new.html.erb
end
end
Am requesting an ideas on how to fix this and get my app working.
You need to update your database migration. Run:
rails g migration AddIdToAsset processmodel_id:integer
rake db::migrate
You've called your attached file :image here:
has_attached_file :image
But you call it :asset in your view:
<%= asset.input :asset, :as => :file, :label => "Image" %>
To fix, just change this line to
<%= asset.input :image, :as => :file, :label => "Image" %>