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
endAnd 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
endAnd, 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
endAnd 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:
Do you have a complete sample app?
I made it working on this starter application some time ago. It might be working:
http://github.com/josevalim/starter
Postar um comentário