Editing a form in nested_form gem

This is a very very naive and simple thing but I quite lost my time on this so I thought might be helpful.

Recently I was using the ‘nested_form’ gem by the great Ryan Bates (https://github.com/ryanb/nested_form). The form to create a new form works like a real gem without any effort.

Now, when I clicked on ‘edit’ link, it created some issues as the nested models didnt come.

I had a simple thing like each question has many answers. When I created a question, I added 5 answers to it but when I edited the question, no answers were visible.

The simple solution for this would be that the <% f.fields_for :answers do |an| %> line in your form would be without an equal to sign so just add it.

Thus, it looks like

<%= f.fields_for :answers do |an| %>

Thats all and it works like a charm!

Hope it helps.

Paperclip Resizing options…

Paperclip allows us to specify image dimensions in the  model iteself. This makes life very simple in cases where we only want image resizing.

Here is a list of things we can do to get the best out of it….

Please note : it applies to image magick also.

The basic specification is <width>x<height> in pixels, optionally followed by a modifier. In some cases you can omit either width or height.

    • 256×256
      This specifies the Maximum dimensions for the image, while preserving the aspect ratio of the image. So if your image were actually 512×256 it would be resized to 256×128 in order to fit inside the specified size.
    • 256×256!
      This specifies the Exact image size, so a 512×256 image would be changed into 256×256, losing the aspect ratio.
    • 256x
      This specifies the desired width of the target image, leaving the height to be set so as to preserve the aspect ratio.

 

    • x256
      This specifies the desired height of the target image, while preserving the aspect ratio.
    • 256×256^
      This specifies the Minimum size for the target image, so the resized image may be larger than these dimensions.
    • 256×256>
      This specifies that the image will be resized only if it is Larger than the dimensions.
    • 256×256<
      This specifies that the image will be resized only if it is Smaller than the dimensions.

Deploying with Pasenger and apache

Recently had come accross the task of deploying a rails application with apache and passenger.

I did everything as prescribed in the way I used to but for some reasons I was shown the directory listing of the application instead of application being executed.

The change I had taken from my conventional way of giving the paths to using symbolic links.

Finally i figured out the solution as an issue with passenger and the way it treats symlinks:

 

here is what you need to put between the <directory> tags to get stuff working:

 

PassengerResolveSymlinksInDocumentRoot on

 

 

Hope it helps !

Why not to use Strings in Cucumber

Cucumber allows you to define step definitions using strings instead of regular expressions.

This might seem simpler at first, but it has other problems, which I’ll illustrate with an example.

Here is a step definition that uses a plain string.

Given “I have 100 in my Account” do

end

We couldn’t write $100 here, because the $ has a special meaning when you define step definitions with strings.

Any $ signs—including any letter or number following it—will be interpreted as an argument, .

This step definition uses an argument:

Given “I have $amount in my Account” do |amount|

 

end

This step definition would match both of the following Gherkin steps:

Given I have 100 in my Account

Given I have $100 in my Account

 

In the first case, the Step Definition’s amount argument would have the value “100”. In the second case it would have the value “$100″. If our Step Definition expects a string with only digits, this can be problematic. We have no way to enforce a consistent way to write Gherkin steps, and the step definitions have to anticipate many kinds of input.

 

This is why using strings instead of regular expressions is not as advantageous as you might think. They give far less control over what gets matched and what arguments a step definition can receive.

Segmentation Fault while doing bundle install

I recently encountered this error of segmentation fault while doing bundle install in one of my rails application.

After a bit of googling I found out its an issue with ruby 1.9.3.

Here is the solution:

 

rvm pkg install iconv
 rvm pkg install openssl
 rvm reinstall 1.9.3 --with-openssl-dir=~/.rvm/usr --with-iconv-dir=~/.rvm/usr

 

Resizing the image without chaning its dimensions using paperclip

Hi,

In a recent application, the need of the hour was to reduce the file size of an image without changing its dimensions.

Now, this can be acheived by reducing the quality of the image.

ImageMagick library has a “convvert” command which does exactly the same..

for eg(on the terrminal) : convert image1.jpg -quality 10 image2.jpg

image1.jpg—>original image

image2.jpg—->final image

 

According to the paperclip wiki there is a :quality parameter which is allowed.

I tried that but for some reasons , I was not able to get it running.

I also tried using :convert_options but it did not work too.

 

Thus , I wrote my own processor and got the stuff working. Here is the model and processor code :

#######Model code###########

class Passet < ActiveRecord::Base
attr_accessible :caption, :markup, :media_passet, :pcontent_id
has_attached_file :media_passet,
:styles => {
: original => {
:geometry => “256×256<“,
:processors => [:qresize]
}
}
belongs_to :pcontent
end

 

###################Processor code########################

make a file called qresize.rb in #{Rails.root}/lib/papaerclip_processors

The path is mentioned because this path is loaded automatically….

module Paperclip
# handles compression of image by reducing its quality
class Qresize < Processor
def initialize file, options = {}, attachment = nil
super
@format = File.extname(@file.path)
@basename = File.basename(@file.path, @format)
end
def make
src = @file
dst = Tempfile.new([@basename, @format])
dst.binmode
begin
parameters = []
parameters << “:source”
parameters << “-quality 10″
parameters << “:dest”
parameters = parameters.flatten.compact.join(” “).strip.squeeze(” “)
success = Paperclip.run(“convert”, parameters, :source => “#{File.expand_path(src.path)}[0]“, :dest => File.expand_path(dst.path))
rescue PaperclipCommandLineError => e
raise PaperclipError, “There was an error during the Size Reduction  for #{@basename}” if @whiny
end
dst
end
end
end

 

This is a general code for image resizing and probably at this stage, we may not require to write a processor.

But the advantage of doing this is that we can extend this to do a lot more as and when requirement comes.

Also, we can handle our video and audio processing by this easily.

The other option is to pass a block(lambda or Proc) to :styles hash but I personally feel this is a much cleaner approach.

 

Hope it helps.

PreProcessing in Paperclip

Hi.

If at all you want to do any kind of prepreocessing on attachments using paperclip then here is a simple(not wen i did it for the first time…) way of getting a TempFile object and doing whatever you want.

Here I set the size of image (actually reduce the original size into half)

 

class Passet < ActiveRecord::Base
attr_accessible :caption, :markup, :media_passet, :pcontent_id
has_attached_file :media_passet,
:styles => {

: original => {
:geometry => Proc.new { |instance| instance.set_size },
:quality => 10
}
}

belongs_to :pcontent

def set_size

ori_img =Paperclip::Geometry.from_file(self.media_passet.queued_for_write[:original].path)

“#{ori_img.width/2}x#{ori_img.height/2}”

end
end

Here I reduce the dimensions if the image and reduce the quality too.

This way, I can save a 1.1MB image as 39.2KB image,

It it what I required,and I hope this will give you a hint about your own application.

 

Hope it helps.

PS: there are many other(mayb elegant solutions) which can be found on paperclip github page whose link I have posted above.