Category: Ruby on Rails

Rails check_box_tag set checked with condition value

There are no ways to do it directly. But the check_box_tag implementation is trivial, you can monkey patch it or create own helper.

Original implementation:

  def check_box_tag(name, value = "1", checked = false, options = {})
    html_options = { "type" => "checkbox", "name" => name, "id" => sanitize_to_id(name), "value" => value }.update(options.stringify_keys)
    html_options["checked"] = "checked" if checked
    tag :input, html_options
  end

Which can be used in our view as following:

label
  - is_broker_selected = params[:search].present? && params[:search][:type].to_s.include?('Broker')
  div[class="icon-checkbox #{is_broker_selected == true ? 'grey' : ''}"]
  i.fa.fa-user
  = check_box_tag('search[type][]', 'Broker', is_broker_selected, class: 'search_type')
  | Broker

 

Reference: http://stackoverflow.com/questions/11796629/rails-check-box-tag-set-checked-with-default-value#_=_

 

Advertisements

Rails-Dynamic-DropDown

I have found many codes and blog posts for dynamic drop down with mysql in rails. But I want to implement it with mongoid in my application. Many codes that I found are not perfectly working for this. Some gave me problem of non appearing data during edit. So I tried to develop my own dynamic drop-down with mongoid in rails. Finally I become success to implement full featured rails dynamic drop down with mongoid and coffeescript.

Find the source code in github:

https://github.com/gkunwar/dynamic-dropdown

Getting Started

First of all you make sure that mongodb is installed.

Clone

If you want to copy and customize the app with changes that are only useful for your own project, you can clone the GitHub repo. You probably should generate the app instead (see below). To clone:

$ git clone git://github.com/gkunwar/dynamic-dropdown.git

Then install the bundle and run the application.

Ruby Arithmetic Operation for Date

I wanted to get the difference between two dates. My date are in format “DD/MM/YYYY”. I tried to get number of days between two date of same format. Finally I got an easy method to get number of day between these two date.

For this first of all I changed these date to date format using following method.

d1 = “12/2/2012”
d2 = “15/2/2012”
d1 = d1.to_date
d2 = d2.to_date
no_of_days = (d2-d1).to_i

Finally, I get number of days between two date. If there are any better methods for this operation, suggestions are welcome.

Using helper in mode;s, views, and controllers

So far, it\'s RoR

Using this as a guide JLHPBv2: Using Helpers in Models in Rails, you can make a single Module that can be accessed from models, views, and controllers. The helpers automatically created in app/helpers/*_helpers.rb   might also be available anywhere as long as you include them.

I decided to make the methods in the helper class-scoped (using self below) so that the origin of the helper method is explicitly obvious in the call (eg. to_s below)

app/lib/utility_helper.rb
module UtilityHelper
  def self.camelcase_to_spaced(word)
    word.gsub(/([A-Z])/, " \\1").strip
  end
end

app/models/event.rb
class Event < ActiveRecord::Base
  include UtilityHelper
 
  def to_s
    UtilityHelper.camelcase_to_spaced("This an Event object")
  end
end

app/helpers/events_helper.rb
module EventsHelper
  include UtilityHelper
end

app/controllers/event_controller.rb
class EventController < ApplicationController
  include UtilityHelper
end


app/views/events/index.html.erb
<%=UtilityHelper.camelcase_to_spaced(event[:event_type])%&rt;

View original post

Configuring SMTP Setting dynamically

SMTP is mail delivery method in rails  which is used to send email through rails. Setting of SMTP is configured in different environment like development, production and test. For this we configure following setting in different file like config/environments/development.rb, config/environments/production.rb, config/environments/test.rb with respect to different environment.

 config.action_mailer.smtp_settings = {
address: “smtp.gmail.com”,
port: 587,
domain: “mail.google.com”,
user_name: “example@gmail.com”,
password: “********”,
authentication: “plain”,
enable_starttls_auto: true
}

But sometime we need to set up smtp attributes like address, domain, port etc. dynamically on run time then these above method gives some problem. Because file on the cofig folder loaded when we start the server. For this we configure smtp settings in application controller. The configuration is as below.

before_filter :set_mailer_settings

private
def set_mailer_settings
options = Mails.first()
ActionMailer::Base.smtp_settings = {
:address => options[“smtp_address”],
:port => options[“smtp_port”],
:domain => options[“smtp_domain”],
:authentication => options[“authentication_type”],
:user_name => options[“smtp_username”],
:password => options[“smtp_password”],
}
end

Where Mails is a model from which attributes of smtp are ftech. By this way we can configure smtp setting dynamically in rails on run time.