2

I want to implement Papereclip in Rails 3.2 without a model.

I am very new to RoR and Paperclip.

I followed through THIS tutorial to use paperclip. It is running successfully.

But in this tutorial they have shown use of the model, I dont want to use the model or dont want to modify the existing model.

4
  • You will need a model to at least save the URL of your image, otherwise what is the point of using Paperclip? You can also check out Ryan Bate's railscasts about paperclip. Commented Nov 6, 2013 at 8:01
  • we need url to return to user in a response of webservice, I dont think we require to store in the database. Commented Nov 6, 2013 at 8:07
  • I am not sure what that means? Commented Nov 6, 2013 at 8:11
  • I'm bit confused, Is it compulsory to store the url in database while using paperclip? Commented Nov 6, 2013 at 8:14

2 Answers 2

3

Rails is object oriented.

# Article model
attr_accessible :image
# Paperclip attachment code and whatever rules and conditions you want.

The image attribute is actually a column from your database and an attribute accessible from your Article model. That image attribute will need to be a string type (varchar) to store the url where the image is saved.

For example if your article has an image attribute, in your view file Article#Show you can display your image through the article object with the image attribute as <%= image_tag @article.image.url %>.

If you want to have many images in a same article then you will want to create 2 models (Article and Image) and have an association where

 # Article model
 has_many :images
 accepts_nested_attributes_for :images

 # Image model
 attr_accessible :image_url
 belongs_to :article
 # paperclip attachments...

With this association you can then fetch and display your images by doing

<% @article.images.each do |image| %>
    <%= image_tag image.image_url.url %>
<% end %>

Technically you don't need to store the url of your image in the database, you can store it in variable, but then it will only be valid during the session and will disappear afterwards. If you want to view your image at a later date, then yes you need to store it in a database.

Sign up to request clarification or add additional context in comments.

2 Comments

How would you store it in a variable?
If you know the address of your image you can store it in a variable and use <%= image_tag my_image_variable %>, where my_image_variable = "http://someurlpath.com/my_image.jpg". This can be done programmatically but your variable will disappear. Otherwise you need to hardcode your variable. In this case image_url (fetch from the database) and my_image_variable are both string type. You also wouldn't need to use paperclip.
1

For not using a seperate model you can check Paperclip Docs. What is basically done here is that the image is stored as an atrribute of an existing model.

About not using a model at all, how do you want to associate the image object to some existing record ? Wont that be required in your project ?

4 Comments

Actually we need give the url in the response of webservice, we don't want to store it in database.
So you will have to persist the url of the image in the db(store in a seperate model or as an attribute of an existing model) so that it can be retrieved to send in the response of the webservice, or am i misunderstanding your goal ?
Yes I think we need separate model to maintain the url, basically I'm new to RoR, so not getting what to do...
You can refer to the Paperclip Wiki. If you are storing only 1 image per record then i suggest you store it as an attribute of an existing model. When you later require to store many images, may be you can refactor your code and create a new Model for Paperclip to use and create associations to store many images per record. I hope this puts you in a right direction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.