How do I access Image.photo using its id from Pin models view?
Currently I am doing this and it gives me random number if I remove the image_tag or bank image space.
<td><%= image_tag Image.find_by_id(pin.pic) %></td>
This one gives me undefined method `photo' for nil:NilClass
<td><%= image_tag Image.find_by_id(pin.pic).photo.url(:small) %></td>
class Pin < ActiveRecord::Base
attr_accessible :board, :pic, :pin_time, :user, :apic, :photo
belongs_to :auser, :class_name => 'User', :foreign_key => 'user'
belongs_to :apic, :class_name => 'Image',:foreign_key => 'pic'
end
class Image < ActiveRecord::Base
attr_accessible :description, :uid, :web_url, :photo, :photo_file_name,:photo_content_type, :photo_file_size, :photo_updated_at, :id
has_attached_file :photo, :styles => { :small => "150x150>", :medium => "300x300", :large => "600x600"}
belongs_to :user
validates_attachment_presence :photo
validates_attachment_size :photo, :less_than => 5.megabytes
validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png']
end
Why not simply
pin.apic.photo.url(:small)
Or are you looking for something else completely?
Related
Below is my code:
class Profile < ActiveRecord::Base
belongs_to :user
validates :first_name, presence: true
validates :last_name, presence: true
has_attached_file :avatar,
:styles => { :medium => "300x300>", :thumb => "100x100>" },
:default_url => ":style/missing.jpg"
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
end
Also, my image is stored in app/assets/images/medium as missing.jpg and in app/assets/images/thumbs as missing.jpg. Right now the error that I get is that it simply displays the word missing and no picture.
Below is the code to display the picture in my view file.
<div class="profile-image">
<%= image_tag #user.profile.avatar.url %>
</div>
I am using activeadmin for car resource and multiple attachments are not entering into records, car records are created successfully but on creation it did not contain attachment. I have two models 'Attachment', models/attachment.rb
class Attachment < ActiveRecord::Base
belongs_to :imageable, polymorphic: true
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
And
and my model/car.rb contains following code
class Car < ActiveRecord::Base
has_many :attachments, as: :imageable
accepts_nested_attributes_for :attachments
end
and in my app/admin/car.rb I have following code for multiple attachment.
form do |f|
f.input :make
f.input :model
f.input :color
f.input :engine_type
f.input :description
f.has_many :attachments do |attachment|
attachment.input :attachment, :as => :file
end
f.actions
end
Can anybody please explain how to fix this issue?
I think your input field should be :avatar instead of :attachment
So, it should look like
f.has_many :attachments do |attachment|
attachment.input :avatar, :as => :file
end
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 want to make polymorphic associations with paperclip, and allow my user to have one avatar and multiple images.
Attachment model:
class Attachment < ActiveRecord::Base
belongs_to :attachable, :polymorphic => true
end
class Avatar < Attachment
has_attached_file :image, :styles => { :thumb => "150x150>", :view => "260x180>" },
end
class Image < Attachment
has_attached_file :image, :styles => { :thumb => "150x150>", :view => "260x180>" },
end
User Model:
has_one :avatar, :as => :attachable, :class_name => 'Attachment', :conditions => {:type => 'avatar'}
accepts_nested_attributes_for :avatar
User Controller:
def edit
#user.build_avatar
end
User View form:
<%= form_for #user, :html => { :multipart => true } do |f| %>
<%= f.fields_for :avatar do |asset| %>
<% if asset.object.new_record? %>
<%= asset.file_field :image %>
<% end %>
<% end %>
when I attempt to save the changes I get the error => unknown attribute: avatar
if I remove the :class_name => 'attachment' in the has_one association I get the error =>
uninitialized constant User::Avatar
I need to also attach avatars to blog posts, so I need the association to be polymorphic (or atleast i think so)
I am stumped and any help would be greatly appreciated.
I do have a project in the works that is successfully using Paperclip and polymorphic associations. Let me show you what I have, and maybe you can apply it to your project:
class Song < ActiveRecord::Base
...
has_one :artwork, :as => :artable, :dependent => :destroy
accepts_nested_attributes_for :artwork
...
end
class Album < ActiveRecord::Base
...
has_one :artwork, :as => :artable, :dependent => :destroy
accepts_nested_attributes_for :artwork
...
end
class Artwork < ActiveRecord::Base
belongs_to :artable, :polymorphic => true
attr_accessible :artwork_content_type, :artwork_file_name, :artwork_file_size, :artwork
# Paperclip
has_attached_file :artwork,
:styles => {
:small => "100",
:full => "400"
}
validates_attachment_content_type :artwork, :content_type => 'image/jpeg'
end
the songs form and the albums form include this as a partial:
<div class="field">
<%= f.fields_for :artwork do |artwork_fields| %>
<%= artwork_fields.label :artwork %><br />
<%= artwork_fields.file_field :artwork %>
<% end %>
don't forget to include :html => { :multipart => true } with the form
artworks_controller.rb
class ArtworksController < ApplicationController
def create
#artwork = Artwork.new(params[:artwork])
if #artwork.save
redirect_to #artwork.artable, notice: 'Artwork was successfully created.'
else
redirect_to #artwork.artable, notice: 'An error ocurred.'
end
end
end
and finally, an excerpt from songs_controller.rb:
def new
#song = Song.new
#song.build_artwork
end
I'm not sure you really need to be polymorphic. How about this approach, which uses has_many :through? In plain English, the user has one avatar which has multiple images, and through this association you can call User.images to get the collection of images associated with the avatar.
http://guides.rubyonrails.org/association_basics.html
class User < ActiveRecord::Base
has_one :avatar
has_many :images, :through => :avatar
end
class Avatar < ActiveRecord::Base
belongs_to :user
has_many :images
end
class Image < ActiveRecord::Base
belongs_to :avatar
has_attached_file :image, :styles => { :thumb => "150x150>", :view => "260x180>" },
end
Having said all of this, I am left to wonder why you need to go through all this anyway. Why not just do
class User < ActiveRecord::Base
has_many :avatars
end
which would give you as many images (avatars) as you want.