I have these two models:
user:
has_many :commercials, :dependent => :destroy, :inverse_of => :user
commercial:
belongs_to :user, :inverse_of => :commercials
has_attached_file :image,
:styles => { :medium => "400x400",
:thumb => "50x50>" }
I just uploaded one image, the image is successfully uploaded to Amazon S3 and saved into the database.
But how to display it?
I tried:
<%= image_tag #user.adverts(:medium) %>
or
<%= image_tag #user.adverts.medium.url %>
But none of above is working...
Not adverts, but commercials? So should it not be something like:
#user.commercials.first.image.url
Assuming foo as the name of the commercial instance
<%= image_tag foo.image.url(:medium) %>
Related
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'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
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
Hey i have a rails app that i am uploading images with paperclip....
So i have a comic_review scaffold that i upload images of comic covers and i also have a news scaffold where i post news and images.....
When i edit news/1/edit and upload a picture the comics_review/1 picture becomes the same as the news one and vice a versa.
I checked the links in the views to make sure that i is correct and they seem to be ok...
heres my view
home.html.erb
<h2 style="color: black; font-size: 30px;"><%= #comic_review1.title %></h2>
<p><%= #comic_review1.content %></p>
<%= link_to 'Read More...', comic_review_path(#comic_review1) %>
<%= image_tag #comic_review1.photo, class: "homepage_comics" %>
news/show.html
<%= #news.author %>
<%= #news.date %>
<%= image_tag #news.photo, class: 'pull-left', style: 'margin-right: 10px;' %>
heres my controller
static_pages_controller.rb
class StaticPagesController < ApplicationController
def home
#user = current_user
#article = Article.first
#comic_review1 = ComicReview.find(1)
#comic_review2 = ComicReview.find(2)
#comic_review3 = ComicReview.find(3)
#comic_review4 = ComicReview.find(4)
end
def comics
#comic_review1 = ComicReview.find(1)
#comic_review2 = ComicReview.find(2)
#comic_review3 = ComicReview.find(3)
#comic_review4 = ComicReview.find(4)
end
def batnews
#article = Article.first
#news_all = News.all
end
end
heres my model
comic_review.rb
class ComicReview < ActiveRecord::Base
attr_accessible :content, :credits, :review, :title, :photo, :comicreview
has_attached_file :photo, :styles => { :small => '400x400' },
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:path => ":attachment/activities/:id/:style.:extension",
:bucket => 'goddam_batman_pics'
has_many :comments, as: :commentable, dependent: :destroy
end
news.rb
class News < ActiveRecord::Base
attr_accessible :title, :author, :date, :content, :photo
has_attached_file :photo, :styles => { :small => '400x400' },
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:path => ":attachment/activities/:id/:style.:extension",
:bucket => 'goddam_batman_pics'
end
controllers are your basic controllers that gets generated by the scaffold
so whenever i update a photo for news/1 the photo for comic_reviews/1 gets the same photo as the news one
any help would be appreciated thank you
I'm pretty sure the problem is that you're giving the "photo" the same name in both models. Try naming one comic_photo and the other news_photo, or alternately, explore Polymorphic Associations.
Dated Railscast link, but gives you the idea - http://railscasts.com/episodes/154-polymorphic-association
PS - Bonus points for figuring out how to load four ComicReviews in one query rather than four in those controllers.
ok so what i had to do was in my model i changed the path from
:path => ":attachment/activities/:id/:style.:extension",
to
:path => "/image/:id/:filename",
and it seems to be working
I'm trying to delete each single image uploaded for a model (work has many images) but now my code works like this: I have three images uploaded in a work, I want to delete just one but when I check the image's checkbox and submit the update action it will delete all the three images.
Here is my work model's code:
before_save :destroy_image?
belongs_to :category
has_many :images, :as => :assetable, :class_name => "Work::Image", :dependent => :destroy
accepts_nested_attributes_for :images, :reject_if => proc { |attributes| attributes['attachment'].blank? }
def image_delete
#image_delete ||= "0"
end
def image_delete=(value)
#image_delete = value
end
private
def destroy_image?
self.images.clear if #image_delete == "1"
end
end
class Work::Image < Asset
has_attached_file :attachment, :styles => {:small => "200x150>", :large => "400x300>"},
:url => "/uploads/works/:id/:style/:basename.:extension",
:path => ":rails_root/public/uploads/works/:id/:style/:basename.:extension"
end
I think the problem is that delete all images
self.images.clear if #image_delete == "1"
but I don't understand how to fix it. I hope in your help.
You should use the :allow_destroy options of accepts_nested_attributes_for instead.
And in your form something like this:
<% #work.images.each do |image| %>
<%= f.fields_for :images, image do |image_fields| %>
<%= image_tag image.url(:small) %>
<%= image_fields.check_box :_destroy %>
<% end %>
<% end %>