quarta-feira, 15 de abril de 2009

And ten days later... SimpleForm goes 0.2

Amazing! SimpleForm was released just 10 days ago it has already 84 followers on GitHub and got a really nice contribution from Andrew Timberlake: attachments support!

How does that work? Simple as it could be (if you do not know what is SimpleForm, I recommend you to read this post first):

class ContactForm < SimpleForm
attribute :screenshot, :attachment => true
end


And then you set your form to multipart, put a file input and you are done. Your contact form now accepts attachments!

Plus, validations can now be symbols. So we can validate our screenshot attachment just when the contact form type is "UI Bug":

class ContactForm < SimpleForm
attribute :type
attribute :screenshot, :attachment => true, :validate => :ui_bug?

def ui_bug?
if self.type == 'UI bug' && self.screenshot.nil?
self.errors.add(:screenshot, "can't be blank when you are reporting an UI bug")
end
end
end


And, finally, to conclude the 0.2 release, you can now have request information on your contact form! Some people talked to me that is useful to have the user agent or remote ip from the user included in the contact e-mail, and I agree with them. So now you can do that:

class ContactForm < SimpleForm
append :remote_ip, :user_agent, :session
end


And when instantiating the form, you just pass the request object:

@contact_form = ContactForm.new(params[:contact_form], request)


And in the contact e-mail received you will have a section with request information, which can be also localized. You can give to append any method that the request object responds to. Holy crap, Batman!

Keep on checking and/or recommend me on Working with Rails if you like what you see. :)

2 comentários:

Mark Richman disse...

Do you have a complete sample app?

José Valim disse...

I made it working on this starter application some time ago. It might be working:

http://github.com/josevalim/starter