Using Rails 3.2 and Paperclip to upload multiple files (photos) at once using HTML5 multipart. Here is my form:
# shops/_form.html.erb
<%= form_for #shop, :url => { :action => action, :type => type }, :html => { :multipart => true } do |f| %>
<%= f.text_field :name %>
<%= f.file_field :shop_photos_data, :multiple => true, :name => "shop[photos_attributes][][data]" %>
<% end %>
It works and yields the following result when updated/created:
{"utf8"=>"✓",
"authenticity_token"=>"9jXvIwcllct7UyUfo6cvhEucQf2u3SY50SuaCLtFO4c=",
"shop"=>{
"name"=>"First shop",
"photos_attributes"=>{"0"=>{
"image"=>[
#<ActionDispatch::Http::UploadedFile:0x00000104b78978
#original_filename="first_test_image.jpg",
#content_type="image/jpeg",
#headers="Content-Disposition: form-data; name=\"gallery[photos_attributes][0][image][]\"; filename=\"first_test_image.jpg\"\r\nContent-Type: image/jpeg\r\n",
#tempfile=#<File:/var/folders/bQ/bQYZC2ukFZCvbKzEDGRtJE+++TI/-Tmp-/RackMultipart20110622-4459-vz78ee>>,
#<ActionDispatch::Http::UploadedFile:0x00000104b78950
#original_filename="second_test_image.jpg",
#content_type="image/jpeg",
#headers="Content-Disposition: form-data; name=\"gallery[photos_attributes][0][image][]\"; filename=\"second_test_image.jpg\"\r\nContent-Type: image/jpeg\r\n",
#tempfile=#<File:/var/folders/bQ/bQYZC2ukFZCvbKzEDGRtJE+++TI/-Tmp-/RackMultipart20110622-4459-1jzhhyg>>
]
}
}
}, "commit"=>"Save", "action"=>"create", "controller"=>"shops"}
It works, and it goes to the shops_controller.rb, but doesn't go into photos_controller.rb.
Here are my other parts of the code:
# photo.rb
class Photo < ActiveRecord::Base
belongs_to :attachable, :polymorphic => true, :counter_cache => true
belongs_to :user, :counter_cache => true
attr_accessible :data, :attachable_id, :attachable_type, :user_id
end
# shop.rb
class Shop < ActiveRecord::Base
attr_protected :photos_count
has_many :photos, :as => :attachable, :dependent => :destroy
accepts_nested_attributes_for :photos, :allow_destroy => true
end
# photos_controller.rb
class PhotosController < ApplicationController
end
# shops_controller.rb
class ShopsController < ApplicationController
before_filter :require_user, :only => [:new, :edit, :update, :create]
...
def update
#shop = Shop.find(params[:id])
if #shop.update_attributes(params[:shop])
flash[:notice] = 'Successfully updated.'
redirect_to shop_path(#shop)
else
render :action => :edit
end
end
end
I have a user_id field in my Photo model. Currently, the user_id is not saved in each new Photo record. What can I do in the shops_controller.rb to include the user_id in the file upload array? I don't want to do it in the form because it exposes security.
Thanks.
Put this in shops_controller.rb
def update
#shop = Shop.find(params[:id])
photos = params[:shop][:photos_attributes]
if !photos.blank?
photos.each do |photo|
photo.merge!(:user_id => current_user.id)
end
end
if #shop.update_attributes(params[:shop])
flash[:notice] = 'Successfully updated.'
redirect_to shop_path(#shop)
else
render :action => :edit
end
end
To add the field to the form use a hidden field like this:
<%= hidden_field_tag "user", #user.id %>
Then in the controller you can access it like this:
params[:user]
Related
I'm somewhat of a newbie with ruby on rails and went off of samurails.com single table inheritance with rails 4 tutorial to add different comment types. This worked great but the problem I'm running into is when I try to use polymorphic associations to get comments and the specific type to function under other models such as project and challenge. A regular comment works, but the specific types do not.
I haven't seen anything that clearly says how to make this work or another option of going about it so any help would be greatly appreciated.
class Comment < ActiveRecord::Base
has_merit
acts_as_votable
belongs_to :commentable, :polymorphic => true
belongs_to :user
belongs_to :commenttype
belongs_to :project
def self.types
%w(Question Idea Problem)
end
def commentable_type=(sType)
super(sType.to_s.classify.constantize.base_class.to_s)
end
scope :questions, -> {where(type: 'Question')}
scope :ideas, -> {where(type: 'Idea')}
scope :problems, -> {where(type: 'Problem')}
end
class Question < Comment
end
class Idea < Comment
end
class Problem < Comment
end
class Project < ActiveRecord::Base
belongs_to :user
has_many :comments, :as => :commentable, :class_name => "Comment"
has_many :questions, :as => :commentable, :class_name => "Question"
has_many :ideas, :as => :commentable, :class_name => "Idea"
has_many :problems, :as => :commentable, :class_name => "Problem"
delegate :questions, :ideas, :problems, to: :comments
end
class CommentsController < ApplicationController
before_action :set_commentable, only: [:index, :new, :create]
before_action :set_type
before_action :set_comment, only: [:show, :edit, :update, :destroy]
def index
#comments = type_class.all
end
def show
end
def new
#comment = type_class.new
end
def edit
end
def create
#comment = #commentable.comments.new(comment_params)
#comment.user = current_user
if #comment.save
redirect_to :back, notice: "#{type} was successfully added."
else
render action: 'new'
end
end
def update
if #comment.update(comment_params)
redirect_to #comment.commentable, notice: "#{type} was successfully updated."
else
render action: 'edit'
end
end
def destroy
#user = current_user
#comment = #commentable.comments.where(comment_user: current_user).first
#commentable.comment.destroy
respond_to do |format|
format.html { redirect_to #commentable, notice: "Comment was deleted." }
format.js
end
end
private
def set_comment
#comment = type_class.find(params[:id])
end
def set_type
#type = type
end
def type
Comment.types.include?(params[:type]) ? params[:type] : "Comment"
end
def type_class
type.constantize
end
def set_commentable
#commentable = find_commentable
end
# add more commentable models here
def find_commentable
if params[:challenge_id]
Challenge.find(params[:challenge_id])
else
end
end
def find_commentable
if params[:project_id]
Project.find(params[:project_id])
else
end
end
def comment_params
params.require(type.underscore.to_sym).permit(:body, :type, :user_id, :commentable_id, :commentable_type, :commentable, :comment_type)
end
end
module CommentsHelper
def sti_comment_path(type = "comment", comment = nil, action = nil)
send "#{format_sti(action, type, comment)}_path", comment
end
def format_sti(action, type, comment)
action || comment ? "#{format_action(action)}#{type.underscore}" : "#{type.underscore.pluralize}"
end
def format_action(action)
action ? "#{action}_" : ""
end
end
<%= form_for [commentable, Comment.new], :html => { :multipart => true } do |f| %>
<%= f.text_area :body, class: "form-control", placeholder: "What's on your mind?" %>
<%= f.label :type %><br>
<%= f.select :type, Comment.types.map {|r| [r.humanize, r.camelcase]}, {}, disabled: #type != "Comment" %>
<%= f.submit "Post", class: "btn pull-right" %>
I have a two model
Gallery
class Gallery < ActiveRecord::Base
has_many :pictures, :dependent => :destroy
accepts_nested_attributes_for :pictures, :allow_destroy => true
attr_accessible :name, :description, :pictures_attributes
validates_presence_of :pictures
end
Picture
class Picture < ActiveRecord::Base
belongs_to :gallery
has_attached_file :image,
:path => ":rails_root/public/images/:id/:filename",
:url => "/images/:id/:filename"
end
And form
<%= form_for #gallery, :html => { :class => 'form-horizontal', multipart: true } do |f| %>
.........
<div class="controls">
<%= file_field_tag "images[]", type: :file, multiple: true %>
</div>
.........
<% end %>
Controller
def create
#gallery = Gallery.new(gallery_params)
respond_to do |format|
if #gallery.save
if params[:images]
params[:images].each { |image|
#gallery.pictures.create(image: image)
}
end
format.html { redirect_to #gallery, notice: 'Gallery was successfully created.' }
format.json { render json: #gallery, status: :created, location: #gallery }
else
format.html { render action: "new" }
format.json { render json: #gallery.errors, status: :unprocessable_entity }
end
end
end
private
def gallery_params
params.require(:gallery).permit(:description,
:name,
:pictures
)
end
If I want to validate has_many pictures image blank?, I got every time a error that Pictures is blank, even I selected images.
How I validate has_many association with multiple image field?
Params
Parameters: {"utf8"=>"✓", "authenticity_token"=>"MCkZFvsYvRndlfaqwXiw5MfhSHGoesawEAPFWzn0Ulw=", "gallery"=>{"name"=>"", "description"=>""}, "images"=>[#<ActionDispatch::Http::UploadedFile:0x007fb7ad9cb7d0 #tempfile=#<Tempfile:/var/folders/r4/jqx7vz8d48z2ky3ndpyzv9vc0000gn/T/RackMultipart20150926-38180-1xrchqg>, #original_filename="yamaha-blaster-BOARD-HEROa.jpg", #content_type="image/jpeg", #headers="Content-Disposition: form-data; name=\"images[]\"; filename=\"yamaha-blaster-BOARD-HEROa.jpg\"\r\nContent-Type: image/jpeg\r\n">], "commit"=>"Create Gallery"}
(0.1ms) begin transaction
(0.0ms) rollback transaction
Your problem lies in your use of accepts_nested_attributes_for - your model reference is to pictures whilst you're referring to images in your code.
Here's how you should do it:
#app/models/picture.rb
class Picture < ActiveRecord::Base
belongs_to :gallery
validates :image, presence: true
has_attached_file :image,
:path => ":rails_root/public/images/:id/:filename",
:url => "/images/:id/:filename"
end
#app/models/gallery.rb
class Gallery < ActiveRecord::Base
has_many :pictures
accepts_nested_attributes_for :pictures, allow_destroy: true
end
You're getting confused about the image object and the Picture model. IE you need to pass the images to the Picture model through your Gallery model:
#app/controllers/galleries_controller.rb
class GalleriesController < ApplicationController
def new
#gallery = Gallery.new
#gallery.pictures.build
end
def create
#gallery = Gallery.new gallery_params
#gallery.save
end
private
def gallery_params
params.require(:gallery).permit(:description, :name, pictures_attributes: [:images])
end
end
This means you're going to have to use f.fields_for, which can be tricky.
Here's how to do it:
#app/views/galleries/new.html.erb
<%= form_for #gallery do |f| %>
<%= f.text_field :name %>
<%= f.fields_for :pictures do |picture| %>
<%= picture.file_field type: :file, multiple: true %>
<% end %>
<%= f.submit %>
<% end %>
This should work for you. The multiple aspect of the upload, I am not sure about.
I am developing a website in ruby on rails. I want to upload multiple images while creating post using paperclip. I could not find any images being uploaded into the public folder and hence images are not getting displayed when I create new post. I have created a model called Postimage.rb for images and referenced to already existing model "post".I have used paperclip to upload single image in other models and it is working fine.
rails g model postimage post:references caption:string
rails g paperclip post_images photo
Here is the Postimage.rb
class Postimage < ActiveRecord::Base
belongs_to :post
has_attached_file :photo,
:styles => {:small => "100x100>", :medium => "640x480>"}
validates_attachment_content_type :photo, :content_type => /\Aimage\/.*\Z/
end
And in the Post model,
class Post < ActiveRecord::Base
has_many :postimages
accepts_nested_attributes_for :postimages, :allow_destroy => true, :reject_if => lambda { |t| t[:postimage].nil? }
Here is the code in post controller
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
respond_to :html
def index
#posts = Post.all.order('created_at DESC').paginate(page: params[:page],:per_page =>7)
#users = User.all
respond_with(#posts)
end
def show
respond_with(#post)
end
def new
#post = Post.new
5.times {#post.postimages.build} # added this
respond_with(#post)
end
def edit
5.times{#post.postimages.build} # ... and this
end
def create
#post = current_user.posts.build(post_params)
#post.user_id = current_user.id
if #post.save
redirect_to #post, :notice =>"Post created successfully!!"
else
render "new"
end
end
def update
#post.update(post_params)
respond_with(#post)
end
def destroy
#post.destroy
redirect_to preschools_Home_path
#respond_with(#post)
end
private
def set_post
#post = Post.find(params[:id])
end
def post_params
params.require(:post).permit(:title, :content, postimages_attributes: [:caption, :photo])
end
Here is the code in _form.html in posts
<%= form_for(#post, :html =>{:multipart => true }) do |f| %>
<%=f.fields_for :postimages do |builder| %>
<%if builder.object.new_record? %>
<p>
<%= builder.label :caption, "Image Caption" %>
<%= builder.text_field :caption %>
</p>
<p>
<%= builder.label :photo, "Image File" %>
<%= builder.file_field :photo %>
</p>
<% end %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Here is the code in show.html.erb in posts
<div>
<% #post.postimages.each do |postimage|%>
<%= image_tag (postimage.photo.url(:medium))%>
<% end %>
</div>
Can you please help me. Thanks.
I have added image column to post model. And that image is getting displayed. But in the case of multiple upload, it is not working.
Logs code below might help.
Parameters: {"utf8"=>"✓", "authenticity_token"=>"PHcPmsB+a0qy6tn4U/nRyycjGFYHiFuum7LNqO8EX24+mGq+Yu7Ct0Ls/odNyAjUtVkT1cMImYRYwnpVajQRXA==", "post"=>{"title"=>"1st post", "content"=>"Added image", "postimages_attributes"=>{"0"=>{"caption"=>"1st image", "photo"=>#<ActionDispatch::Http::UploadedFile:0x007f4a351b0ba8 #tempfile=#<Tempfile:/home/ubuntu/workspace/website/RackMultipart20150715-1790-7hfszv.jpg>, #original_filename="Murali.jpg", #content_type="image/jpeg", #headers="Content-Disposition: form-data; name=\"post[postimages_attributes][0][photo]\"; filename=\"Murali.jpg\"\r\nContent-Type: image/jpeg\r\n">}
The solution provided below works with Paperclip as well:
Rails 4 multiple image or file upload using carrierwave
Only difference would be in post_attachment.rb
Instead of:
class PostAttachment < ActiveRecord::Base
mount_uploader :avatar, AvatarUploader
belongs_to :post
end
You would have
class PostAttachment < ActiveRecord::Base
belongs_to :post
has_attached_file :avatar, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/
end
Getting this error in posts>show when I try to load up a form.
The form is for users to send messages to each other. Grateful for any feedback!
Thanks.
NoMethodError in Posts#show
Showing /Users/fkhalid2008/loand/app/views/posts/show.html.erb where line #1 raised:
undefined method `user' for #<Post:0x12e2ae930>
Extracted source (around line #1):
1: <%= form_remote_tag (:update => 'message', :url => {:controller => 'main', :action => 'send_message', :user_id => #post.user.id}) do %>
2: <br>
3: <br />
4: <br />
POSTS>SHOW VIEW
<%= form_remote_tag (:update => 'message', :url => {:controller => 'main', :action => 'send_message', :user_id => #post.user.id}) do %>
<br>
<br />
<br />
<div class="field">
Hello! My name is <%= f.text_field :subject %> and I'm contacting you in response to your ad. I'm interested in learning more so get in touch! Here's my contact details: <%= f.text_field :body %>.
</div>
<button type="submit" class="btn span6 large">Submit</button>
<% end %>
POSTS CONTROLLER
def new
#post = Post.new
respond_to do |format|
format.html # new.html.erb
format.json { render :json => #post }
end
end
def edit
#post = Post.find(params[:id])
end
def create
#post = Post.new(params[:post])
respond_to do |format|
if verify_recaptcha && #post.save
format.html { redirect_to :action=> "index"}
format.json { render :json => #post, :status => :created, :location => #post }
else
format.html { render :action => "new" }
format.json { render :json => #post.errors, :status => :unprocessable_entity }
end
end
end
MAIN CONTROLLER
class MainController < ApplicationController
def send_message
message = Message.new
message.subject = params[:subject]
message.body = params[:message]
message.sender = User.find session[:user]
message.recipient = User.find params[:user_id]
if message.save
ContactMailer.deliver_message_email message.recipient.email, message.id, request.host
return redirect_to "/posts"
else
render :text => "Hmm. Something seems to be wrong...let me look into it"
end
CONTACT MAILER
class ContactMailer < ActionMailer::Base
def message_email (recipient, id, host)
recipients [ recipient ]
subject "Weddoo: You have received a new message"
from "admin#weddoo.com"
bcc ["admin#weddoo.com"]
content_type "text/html"
body :message_id => id, :host => host
sent_on Time.now
end
end
end
ROUTES.RB
Mysalary::Application.routes.draw do
resources :users do
resources :messages
end
resources :profiles
resources :pages
resources :posts
get "pages/home"
get "pages/about"
get "pages/legal"
get "pages/feedback"
root :to => 'posts#new'
end
POST MODEL
class Post < ActiveRecord::Base
attr_accessible :title, :job, :location, :salary
validates :title, :job, :location, :salary, :presence => true
validates :salary, :numericality => {:greater_than_or_equal_to => 1}
default_scope :order => 'posts.created_at DESC'
end
The error is saying there is no method user on the Post model. It points you directly to the first line.
<%= form_remote_tag (:update => 'message', :url => {:controller => 'main', :action => 'send_message', :user_id => #post.user.id}) do %>
so I'd say
#post.user.id is your problem statement, that model doesn't have a method user but it's hard to know for sure without seeing the Post model.
I think you have missed to add relationship.
class Post < ActiveRecord::Base
belongs_to :user
end
I'm working on a website that allows people who run bed and breakfast businesses to post their accommodations.
I would like to require that they include a "profile image" of the accommodation when they post it, but I also want to give them the option to add more images later (this will be developed after).
I thought the best thing to do would be to use the Paperclip gem and have a Accommodation and a Photo in my application, the later belonging to the first as an association.
A new Photo record is created when they create an Accommodation. It has both id and accommodation_id attributes. However, the image is never uploaded and none of the Paperclip attributes get set (image_file_name: nil, image_content_type: nil, image_file_size: nil), so I get Paperclip's "missing" photo.
Any ideas on this one? It's been keeping me stuck for a few days now.
Accommodation
models/accommodation.rb
class Accommodation < ActiveRecord::Base
validates_presence_of :title, :description, :photo, :thing, :location
attr_accessible :title, :description, :thing, :borough, :location, :spaces, :price
has_one :photo
end
controllers/accommodation_controller.erb
class AccommodationsController < ApplicationController
before_filter :login_required, :only => {:new, :edit}
uses_tiny_mce ( :options => {
:theme => 'advanced',
:theme_advanced_toolbar_location => 'top',
:theme_advanced_toolbar_align => 'left',
:theme_advanced_buttons1 => 'bold,italic,underline,bullist,numlist,separator,undo,redo',
:theme_advanced_buttons2 => '',
:theme_advanced_buttons3 => ''
})
def index
#accommodations = Accommodation.all
end
def show
#accommodation = Accommodation.find(params[:id])
end
def new
#accommodation = Accommodation.new
end
def create
#accommodation = Accommodation.new(params[:accommodation])
#accommodation.photo = Photo.new(params[:photo])
#accommodation.user_id = current_user.id
if #accommodation.save
flash[:notice] = "Successfully created your accommodation."
render :action => 'show'
else
render :action => 'new'
end
end
def edit
#accommodation = Accommodation.find(params[:id])
end
def update
#accommodation = Accommodation.find(params[:id])
if #accommodation.update_attributes(params[:accommodation])
flash[:notice] = "Successfully updated accommodation."
render :action => 'show'
else
render :action => 'edit'
end
end
def destroy
#accommodation = Accommodation.find(params[:id])
#accommodation.destroy
flash[:notice] = "Successfully destroyed accommodation."
redirect_to :inkeep
end
end
views/accommodations/_form.html.erb
<%= form_for #accommodation, :html => {:multipart => true} do |f| %>
<%= f.error_messages %>
<p>
Title<br />
<%= f.text_field :title, :size => 60 %>
</p>
<p>
Description<br />
<%= f.text_area :description, :rows => 17, :cols => 75, :class => "mceEditor" %>
</p>
<p>
Photo<br />
<%= f.file_field :photo %>
</p>
[... snip ...]
<p><%= f.submit %></p>
<% end %>
Photo
The controller and views are still the same as when Rails generated them.
models/photo.erb
class Photo < ActiveRecord::Base
attr_accessible :image_file_name, :image_content_type, :image_file_size
belongs_to :accommodation
has_attached_file :image,
:styles => {
:thumb=> "100x100#",
:small => "150x150>" }
end
To create an upload with paperclip, you need to use the name you provided for the has_attached_file line, on the model you defined it on. In your case, this will result in this view code:
<%= form_for #accommodation, :html => { :multipart => true } do |f| %>
<%= f.fields_for :photo do |photo_fields| %>
<p>
Photo<br />
<%= photo_fields.file_field :image %>
</p>
<% end %>
<% end %>
In the controller:
class AccommodationsController < ApplicationController
# also protect create and update actions!
before_filter :login_required, :only => [ :new, :create, :edit, :update ]
def new
# always make objects through their owner
#accommodation = current_user.accommodations.build
#accommodation.build_photo
end
def create
#accommodation = current_user.accommodations.build(params[:accommodation])
if #accommodation.save
# always redirect after successful save/update
redirect_to #accommodation
else
render :new
end
end
end
Tell Rails to handle the nested form:
class Accommodation
has_one :photo
accepts_nested_attributes :photo
attr_accessible :photo_attributes, :title, :description, :etc
end
And make sure to set the accessible attributes right in your photo model:
class Photo
attr_accessible :image # individual attributes such as image_file_name shouldn't be accessible
has_attached_file :image, :styles => "etc"
end
Be sure to watch your log files to spot things that are protected by attr_accessible, but still are in your form.