Thursday, October 31, 2013

OnHover Dropdown menu in Bootstrap

Bootstrap DropDown menu works on mouse click event. It doesn't support on hover DropDown by default. But using following bootstrap plugin we can make it works for mouse hover event and it's so simple.

http://cameronspear.com/blog/bootstrap-dropdown-on-hover-plugin/



Download the plugin, include it in your project. We write data-toggle="dropdown" in the markup to make it a dropdown menu which works on click event. Instead of that one, write data-hover="dropdown" and you are done. Now both options will work on your project.

Tuesday, September 17, 2013

Uploading file using AJAX in Rails

Paperclip gem can be easily used in your rails application for uploading file. But to make it functional using ajax, we can use remotipart gem which works pretty smoothly with Paperclip gem. Just install the remotipart gem. Prepare your form using Paperclip for uploading file. Then add remote=>true in that form. And your form is ready to upload file using ajax. Implementation details can be found in the following links.

Thursday, September 12, 2013

Attachment in Rails using Paperclip gem

It always requires extra effort to setup your form for uploading files. Paperclip is a wonderful gem which can save your extra effort for that task. Just install the gem, add necessary fields in model including file field, and last of all add multipart => true in your form. Then your application is ready for uploading file. Most of the required options related to uploading file are available in this gem. You can find implementation details in following links.

Friday, August 30, 2013

Tagging in Rails

A very simple plugin to implement tagging feature in rails application is acts-as-taggable-on. Install the gem, specify the field in the model on which you want to put tags, add a tag field in the form, and you are ready to tag your model object. Implementation details can be found in following links.

https://github.com/mbleigh/acts-as-taggable-on

http://railscasts.com/episodes/382-tagging
http://alexmuraro.me/posts/acts-as-taggable-on-a-short-tutorial/

Popularity of this plugin can be found in The Ruby Toolbox -
https://www.ruby-toolbox.com/categories/rails_tagging

Limiting loop for array of objects in Rails


While fetching records from DB, we can definitely limit the number of records to fetch as following.
@post.comments.limit(10).each do |comment|
   comment.description
end
But in practical, sometimes we face that we have an array of objects fetched from DB and we want to access first or last few objects. Using following code we can easily solve it. 

Reading first 10 records -
@posts.first(10).each do |post|
   post.title
end
Reading last 10 records -
@posts.last(10).each do |post|
   post.title
end