Tuesday, March 3, 2009

Calculate date difference in ror

For example if you have a @date1 and @date2, and you want a difference between them

@date1 = {firstdate}
@date2 = {Seconddate}
@diff = (@date2.to_date - @date1.to_date).to_i

And you will get a difference in integer. :)

The code given below is explaining the subtraction of timestamp and date.

A timestamp is probably a Time and subtraction gives the number of
seconds between the two. If you convert to Date, subtraction gives
days.

>> Time.now - 18.seconds.ago
=> 17.999993
>> Time.now - 3.days.ago
=> 259199.999989

>> Date.today
=> #
>> Date.today.to_s
=> "2007-06-18"
>> 3.days.ago.to_date.to_s
=> "2007-06-15"

>> Time.now.to_date - 3.days.ago.to_date
=> Rational(3, 1)
>> (Time.now.to_date - 3.days.ago.to_date).to_i
=> 3
>> (Date.today - 3.days.ago.to_date)
=> Rational(3, 1)
>> (Date.today - 3.days.ago.to_date).to_i
=> 3

Monday, March 2, 2009

Action specific Layout in ROR

Very Easy:
Use this in Your Controller

layout 'layout_name',:except => [:show,:new]

Easy Exceptions handling in ROR

Use this in your application.rb file :
rescue_from NameError, :with => :handle_exceptions
rescue_from NoMethodError, :with => :handle_exceptions
rescue_from ActiveRecord::Rollback, :with => :handle_exceptions
rescue_from ActiveRecord::StatementInvalid, :with => :handle_exceptions
rescue_from ActiveRecord::RecordNotFound, :with => :handle_exceptions
rescue_from ActionController::UnknownAction, :with => :handle_exceptions
rescue_from ActiveRecord::RecordNotFound, :with => :handle_exceptions
rescue_from ActiveRecord::StaleObjectError, :with => :handle_exceptions
rescue_from ActiveRecord::RecordInvalid, :with => :handle_exceptions
rescue_from ActiveRecord::RecordNotSaved, :with => :handle_exceptions
rescue_from ActionController::MethodNotAllowed, :with => :handle_exceptions
rescue_from ActionController::MethodNotAllowed, :with => :handle_exceptions
rescue_from ActionController::InvalidAuthenticityToken, :with => :handle_exceptions

private

def handle_exceptions
render :action => '500.html',:text => 'This is an error', :status => 500
end

Display keys and values of hashes

Well before reading this Post let me tell you that author of this Idea is My friend "Michael Graff"

<% @hash.each do |key, value| %>
<%=h "#{key.to_s} #{value.to_s}" %>
<% end %>

You can iterate through hashes in many various ways, including sorting them:

<% @hash.keys.sort.each do |key| %>
<%=h "#{key.to_s} #{@hash[key]}" %>
<% end %>

Line breaks in textarea in Ruby

Well it is very simple although i spent a lot of time on this but I got the solution which is very simple.


<%= (@object.details).gsub("\n","\
") %>

note:'do not use \ in your code that i used here \
, just simply use break tag'

what I was doing is

<%=h (@object.details).gsub("\n","\
") %>

the last line I mentioned here is using a <%=h which is generated by scaffold. So dont use this <%=h, simply use <%= and gsub method and it will work fine.

Note: using <%=h ... %> ensures that any HTML contained within would not render directly into the page, but instead convert <>

RestFul_authentication Plugin

How to download and install restFul_authentication..

follow these steps
script/plugin source http://svn.techno-weenie.net/projects/plugins

script/plugin install restful_authentication

script/generate authenticated user sessions

NAMEERROR IN SESSIONSCONTROLLER WHILE USING RESTful_authentication

NAMEERROR IN SESSIONSCONTROLLER#CREATE
uninitialized constant SessionsController
RAILS_ROOT: C:/rails/bookmarks
Application Trace | Framework Trace | Full Trace
REQUEST
Parameters:
{"commit"=>"Log in",
"authenticity_token"=>"0c0f07222da8ed58d8c6ebcafb9bf32f0bc84143",
"login"=>"anthony",
"password"=>"mysupersecretpassword"}
Uh oh! what went wrong? It looks like Rails is looking for a controller called SessionsController. But we generated one
named SessionController. Why is this happening? Take a look in the views/session/new.html.erb file.
<% form_tag session_path do -%>


<%= text_field_tag 'login' %>



<%= password_field_tag 'password' %>


<%= submit_tag 'Log in' %>


<% end -%>
The form_tag target is session_path. session_path is created for us by magic because of its mapping in routes.rb.
map.resource :session
When mapping a resource like this Rails looks for the controller with the pluralized name of the resource name by default,
which means session_path is getting SessionsController. We can change the behavior by explicitly setting the controller for the resource.

map.resource :session, :controller => :session
Try to log in again and you will be authenticated! Of course it may just be easier to remember to use the pluralized name of whatever controller you want to use for managing sessions.