Monday – still having rmagick problems.

Got rmagick installed – the problem was the the latest windows version of the gem was actually broken and nobody bothered to mention it or change it. Only found out off a forum that it was broken. They pointed me at an earlier one which did install fine in the usual way.

To get it to work, i added these lines to environment.cfg:

require ‘rubygems’
require ‘RMagick’
include Magick

Next, according to instructions here, i added this line to my newspipe controller:

imagemagick_for ‘/data/pictures/’, :cache => ‘/data/pictures/cache’


god damn it.  I had loads of stuff about image displaying hassles here.  OK, here’s a summary instead.

I have the above in the controller and this in the view:

<%= imagemagick_tag “pic#{}.#{picture.content_type.split(“/”).last}” %>

It’s making the picture name (eg pic32.jpeg) ok but isn’t displaying it because it keeps looking in appname/imagemagick for the file.  It’s like it’s ignoring the imagemagick_for line.  Maybe it’s in the wrong controller?  Maybe it needs to be in a special controller for pictures or something?

image_tag is working now, since i found out that any data that i read and write in my app needs to be in the appname/public folder.  What i’m doing now is using imagemagick to save a thumbnail of the picture alongside the picture file, prefixed with the word ‘thumb’: this is a method of Picture so lives in picture.rb:

def write_to_disc
#save the picture to newspipe/data/pictures with the name ‘#{}.extension’
new_filename = “pic#{}.#{self.content_type.split(“/”).last}”“#{RAILS_ROOT}/public/data/pictures/#{new_filename}”, “wb”) { |f| f.write(self.file_data) }
#now save a thumbnail version as well
thumb = MiniMagick::Image.from_file(“#{RAILS_ROOT}/public/data/pictures/#{new_filename}”)
thumb.resize “200×200”
return true

I’m calling this as part of the create_picture method in the controller.

When it comes to displaying the pictures, i’m showing the thumbs in the view with:

<%= image_tag “/data/pictures/thumbs/thumbpic#{}.#{picture.content_type.split(“/”).last}”, :alt => “Missing thumbnail” %> 

So, i’m showing the name and thumbnail of every picture belonging to the story.  Currently in a very messy way.  I need to a) sort the messiness out and b) make the name and the thumbnail link to that picture’s ‘show_picture’ page.  Shouldn’t be hard.  c) add a ‘delete picture’ link to the side of the picture name.

a) is done.

b) Remembered that i was going to show pictures and movies in the viewer to the right of the pic and movie list.  Hmm.  Thinking about that more, it places a severe limit on the size of the picture.  I think it’s best left as a movie viewer, as limiting the size (on the screen) of movies is reasonable.  So, pictures get their own page.

So, the controller’s pretty simple: we just need to get something into @picture:

  def show_picture
@picture = Picture.find(params[:id])

Next, the view.  Ideally there should be the same view_picture page whether you’ve come from show_story or edit_story.  That means that the ‘delete picture’ option should only be available if session[:user] is the same as @picture.user, and if we came from edit_story (since deleting pictures falls within the bounds of editing a story, i don’t want to give the option if the user was just viewing the story.  if i change my mind about this later it’s easy to alter the code).

It also means i need to remember where the user came from (show_story or edit_story) – this will need to go into params when show_picture is called.  The second bit was easy:  when i call show_picture i pass through a parameter called :came_from, which is set to the action i’m calling from.  (is this passed through automatically in rails?)

Next, the actual deletion.  Looking at the app, i realised i don’t have any deletion capabilities anywhere at the moment – articles, stories etc can’t be deleted.  Need to go back and look in another app to remember how to do it…

Ok, it’s easy – we just have an action called destroy which calls destroy on the object in question and redirects to where we want to go.  I’m going to do a different destroy action for pictures, movies, articles etc as the logic of what to do next may differ.

OK, done that, nice and easy (damn it’s good to be making progress again).  The controller action (note that we need to make a note of what story to go back to before we destroy the picture, but i can just pull the place to go back to out of the params – this means that i can add the ability to delete pictures from other places without this breaking):

def delete_picture
@picture = Picture.find(params[:id]).destroy
@story = @picture.story
flash[:message] = “Picture #{params[:id]} deleted”
redirect_to :action => params[:came_from], :id => @story

and the link in view

<%= link_to ‘Delete this picture’, :action => ‘delete_picture’, :id => @picture, :came_from => params[:came_from] %>  

Added that next to the picture title in the edit_story page as well. (substituting picture for @picture).

Cool.  So i can now add pictures to a story, show a list of thumbnails, view them full size, and delete them.  It’s 8.30 so that seems like a good time to stop and have dinner.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s