Sunday, October 18, 2015

Bootstrap datepickers on Rails application


I recently used bootstrap datepicker for selecting dates. As a gem, I used bootstrap3 datepicker rails for my rails application. I needed the datepicker and time picker separately. So, I had two input fields, one just as calendar and another for time. This blog is about the datepicker. The timepicker will follow shortly. To use this gem, I added following to my Gemfile:
gem 'momentjs-rails', '>= 2.9.0'
gem 'bootstrap3-datetimepicker-rails', '~> 4.14.30'
In the view, I include the datepicker as:
<div class="form-group col-md-6">
   <%= label_tag "date", "Date"%>
   <%= text_field_tag "date", nil, class:"form-control", placeholder: "From", required: true %>
   <span class="input-group-addon">       </span >
</div >
The above code shows only how the calendar will be displayed. To utilize the functionality of the calendar, we have to call datetimepicker() function of javascript. To use datepickers only as calendar use the following script:
<script>
$(document).ready(function() {
$(".date").datetimepicker({
viewMode: 'days',
format: 'DD/MM/YYYY'
});
});
</script>
It does the following:
1. Give the date with format day/month/year. If the format is not selected, the calendar gives both calendar and time selection options.
2. Does not show time option so that you can only use the calendar.
Other lot of options are possible to configure the calendar. Please refer to the documentation if you intend to use it. I have added a small datetimepicker project so that it gives you an idea about how to use it in your rails application.

Saturday, September 17, 2011

Playing with css: working with position absolute

If you always want an element at a certain place without relativity to any other element, then we use this property "position:absolute;". While playing around with "position absolute", I learned that we do not use "margin", instead we use properties such as "top, bottom, right and left".

For instance, I have an element with class "example", I use "position absolute" in it as:
Hello

We can also give "float" values to "absolute positions" like
float:left or float:right

Pfeed for mongoid

As pfeed was originally for ActiveRecord, I had some problem integrating it in the mongoid. However, I forked the original pfeed and made it compatible for mongoid. The modified version of pfeed for mongoid can be found https://github.com/sadiksha/pfeed. This is suitable for mongoid version 2.0.2 and rails 3.0.0.

Saturday, November 6, 2010

Cucumber test for select box!

The behavior driven development is really fun. First of all you determine the flow of the program and then only write real codes. This technique is really effective as it saves time while programming, since the flow is already determined, the chance of hindrance is very low.

A simple test for seeing something in select box is:
Given I am on the index page
And the "Nepal" should be selected for "Country"
(where "Nepal" is your selected value and "Country" is the id/label of the select box)

The step definition for this step is
Then /^"([^\"]*)" should be selected for "([^\"]*)"$/ do |selected_value, field|
find_by_id(field).value.to_s.should == selected_value
end

Using ajax call with file field!

Sometime ago I had real trouble using file field and ajax call together. I wanted to browse a file and display the contents in the same page. Then after some research I came across gem called "remotipart". This gem is really helpful.

To use this gem follow these simple steps
1. Include "gem remotipart" in your Gemfile
2. Bundle install
3. type this in your console: rails generate remotipart
4. Include jquery-1.4.2-min.js, rails.js, jquery.form.js and jquery.remotipart.js as your javascript files
5. Suppose you are using this in you new.html.erb file, then do this
<%= form_for @product, :html => { :multipart => true }, :remote => true do |f| %>
<%= f.label :file_upload %>
<%= f.file_field :file_upload %>
<%= f.submit %>
<% end %>
6. In your create controller include: format.js {} instead of format.html{}
7. In create.js.erb file :
<%= remotipart_response do %>
//Write your javascript code here.
<% end %>

If you have any confusions go to https://github.com/formasfunction/remotipart. This site really helped me a lot.

Sunday, September 19, 2010

Using cygwin for rails!

My previous posts helped you only to install cygwin. But this time, I tried using cygwin for all rails commands.

The first step is to install rvm through your cygwin bash shell. RVM is a ruby version manager that allows you to have multiple versions of ruby installed. So, when you need to use ruby-1.8.7 or ruby-1.9.2 for certain projects you can shift from one to another.

I tried using curl command to install rvm but I was unsuccessful. So, I installed rvm as a gem. By typing the command

~ gem install rvm

And copied:

function gemdir {
if [[ -z "$1" ]] ; then
echo "gemdir expects a parameter, which should be a valid RVM Ruby selector"
else
rvm "$1"
cd $(rvm gemdir)
pwd
fi
}
in my .bash_rc file also you need to copy:

[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # This loads RVM into a shell session.

into your .bash_profile.

If you chose ruby initially when installing cygwin, the version of the ruby will be 1.8.7. However, the most current version of ruby is 1.9.2-p0. So, if you don't want to be lagging behind. In your cygwin shell, type
~ rvm install ruby-1.9.2-p0

You can see whether the rvm installed latest ruby by
~ rvm list

(for more commands of rvm try ~rvm usage)

Though you have installed latest ruby, you need to tell rvm to use that version of ruby. So,
~ rvm default 1.9.2-p0

Also you would want to use global gemset because global allows you to share gems for that version of ruby. For example, if you have two gemsets, say, projectone and projecttwo. However, they may require same gems, devise (for user login). In this case, if your global gemset has devise, then both the gemsets "projectone" and "projecttwo" can have access to that gem. So, for using global gemset
~ rvm gemset global

Similarly you can create gemsets by
~ rvm gemset create projectone

Now you would like to install gems for latest ruby. So download "rubygem 1.3.7" from in "path\to\Cygwin\home\Home\.rvm\gemsets\ruby". Also install latest rails by
~ gem install rails --pre

(In my case, the last command installed rails-3.0.0.rc2 which is not latest. So, after the rc2 was installed I tried "~ gem update rails" and it worked!)

I am using cygwin for rails commands because sometimes ruby interpreter does not allow you to install several gems however, this caveat is overcomed by cygwin. I hope this post helped you in installation.

For more information on using rvm visit beginrescueend.com, it will surely solve all your confusions if you have any regarding rvm. Have fun with rvm! ;)

Sunday, September 12, 2010

Going deep in rails!

The previous blogs were only theoretical. To learn ruby on rails in details please visit http://edgeguides.rubyonrails.org/. Follow the guide and you will have clear concept on programming in ruby on rails.