blabldfseges
blabldfseges

Reputation: 133

Add Image to a table Ruby on Rails

I'm a RoR beginner and I can't seem to find a tutorial on the following thing:

I want to add a picture for each of the tv shows in my database and then want to display the image by calling @show.image in my view (show being a tv show). First, what's the column content ? (I would assume a string, but I'm thinking there might be something more appropriate) Second, is there a way that I won't need to add URL's manually to the DB ? Like if I could somehow specify that the image name is the same as the tv show's name ?

My questions might be a bit confusing so please let me know if you cant understand what I mean!

Cheers

Upvotes: 2

Views: 4246

Answers (2)

dpassage
dpassage

Reputation: 5453

For the first part of your question, I'm assuming that you want to store the URL's of the images in the database, and not the images themselves. string is a fine column type to use for this.

On the second part - yes, you can generate the image filename from other data if you want to. Probably the best way to do this is in the model, maybe with something like this:

def image
  "/images/#{self.name}.png"
end

Then in your view, you can just call @show.image in the template.

Upvotes: 0

Manjunath Manoharan
Manjunath Manoharan

Reputation: 4617

You need to use a gem called paperclip, it is very good.

Here is a link to the railscast

The railscast is pretty old, so the paperclip is a plugin, which won't work with rails 3.2 + . The below github link will be of better use

Also the gem is open source and you can see this on github

Upvotes: 1

Related Questions