Play MP3 audio files in rails

By , June 18, 2009 11:43 pm

If  you want to play mp3 files from your web application then you can do it by using SoundManager2 plugin.

There are few steps that you need to follow.

Step 1: Install the soundmanger2 plugin.

ruby script/plugin install http://soundmanager2.rubyforge.org/svn/soundmanager2

Step 2:  Generate the soundmanager2 in your app. This will store the necessary files in your app.

ruby script/generate sound_manager2

Above command give following result.

public/soundmanager2.swf
public/javascripts/soundmanager2.js
public/javascripts/soundmanager2-jsmin.js
public/javascripts/soundmanager2-rails.js
public/images/SoundManager2/pause-control.gif
public/images/SoundManager2/play-control.gif

Step 3: Add below code in your view file.

<%=javascript_include_tag :defaults%>
<%=sounds=[[sound1,"<MP3 FILE URL>"][sound2,"<MP3 FILE URL>"]]%>

<%=initialize_sounds(sounds) %>

Play sound 1: <%= toggle_sound "sound1" %>
Play sound 2: <%= toggle_sound "sound2" %>

Now run view file and you will get Start and stop button to play mp3 file.

AJAX Pagination in rails

By , June 17, 2009 4:29 am

Visit more proper way ajax pagination in rails:  Click here

Pagination is a very common feature of web application development. Sometimes we need pagination with ajax.

I found lot of blogs and discussion on this but none of them provide better and easy way solution.

Finally I found one of easy solution based on will_paginate gem. For this you have apply below patch in will_paginate/lib/view_helper.rb file

def page_link_or_span(page, span_class, text = nil)
text ||= page.to_s
classnames = Array[*span_class]
if page and page != current_page
if @options[:update]
@template.link_to_remote text, :update => @options[:update], :url =>url_for(page)
else
@template.link_to text, url_for(page), :rel => rel_value(page), :class => classnames[1]
end
else
@template.content_tag :span, text, :class => classnames.join(' ')
end
end

Now add  below code in your view file.


<%= will_paginate @users, :update=>'DIV_ID',:params=>{:controller=>'CONTROLLER_NAME',:action=>'ACTION_NAME'},:container => false %>

Let me know if you have any better solution for this or you faced any problems in this.

Import contacts list in rails

By , June 15, 2009 1:42 am

Nowadays “Invite a friends” functionality is very common in all websites. This functionality is consider as quickest and easy way marketing approch.

MySpace, Twitter sites are using contact imported tool which is load ur all contacts (You need to just provide email and password and select the emails).So users can easily invite their friends without too much hard work.

Now you can do same thing by integrate Plaxo in your web site.you have to just follow few steps.

Step 1: Create a view page in your app and following code in that (Example: contact.html.erb)


<script type="text/javascript" src="http://www.plaxo.com/css/m/js/util.js"></script>
<script type="text/javascript" src="http://www.plaxo.com/css/m/js/basic.js"></script>
<script type="text/javascript" src="http://www.plaxo.com/css/m/js/abc_launcher.js"></script>
<script type="text/javascript"><!--
function onABCommComplete() {
// OPTIONAL: do something here after the new data has been populated in your text area
}
//--></script>

Step 2: Create a another view page (Example: contact_list.html.erb). This page is used to load the contacts in text area.

<html>
<head>
<script type="text/javascript" src="https://www.plaxo.com/ab_chooser/abc_comm.jsdyn"></script>
</head> <body></body> </html>

Step 3: Add below in contact.html.erb file


<textarea id="recipient_list" name="recipients"></textarea>.
<a href="#" onclick="showPlaxoABChooser('recipient_list', '/CONTROLLER_NAME/contact_list'); return false">
<img src="http://www.plaxo.com/images/abc/buttons/add_button.gif" alt="Add from my address book" />
</a>

Remember: In showPlaxoABChooser , first argument is textarea id and second argument is callback page (here contact_list page).

Till this process , you can only able to import the contacts into textarea. I hope you know that how to send email after that.

Let me know if you find any problems.

Integrate twitter oAuth in your rails application

By , June 11, 2009 12:11 am

Now Twitter is using oAuth service to intergrate with your web application. There are the few steps that you need to follow to use it.

First of all you need to get your consumer key and consumer secret. You will get these details by register your application on twitter.

After getting consumer key and consumer secret, you need to install oauth gem.

$ sudo gem install oauth
Password:
Successfully installed oauth-0.2.7
1 gem installed
Installing ri documentation for oauth-0.2.7...
Installing RDoc documentation for oauth-0.2.7...

Now create a new application.

rails twitteroauth
cd twitteroauth

Create User scaffold in your application.

ruby script/generate scaffold user screen_name:string token:string secret:string
rake db:create
rake db:migrate

Add below code in UserController

Note: Because latest oauth gem, changes made in create and callback method on 29/04/2010
def self.consumer
# The readkey and readsecret below are the values you get during registration
OAuth::Consumer.new("YOUR CONSUMER KEY", "YOUR CONSUMER SECRET",{ :site=>"http://twitter.com" })
end
def create
@request_token = UsersController.consumer.get_request_token(:oauth_callback => "callback-url-of-your-twitter-app")
session[:request_token] = @request_token.token
session[:request_token_secret] = @request_token.secret
# Send to twitter.com to authorize
redirect_to @request_token.authorize_url
return
end

This action gets a request token from Twitter and then redirects the user to Twitter to authorize access. This action does not call User.new, that is delayed until the user returns from the Twitter authorization.
In the registration above we registered the callback URL as /users/callback, so let's add that action:

def callback
@request_token = OAuth::RequestToken.new(UsersController.consumer,
session[:request_token],
session[:request_token_secret])
# Exchange the request token for an access token.
@access_token = @request_token.get_access_tokenn(:oauth_verifier => params[:oauth_verifier])
@response = UsersController.consumer.request(:get, '/account/verify_credentials.json',
@access_token, { :scheme => :query_string })
case @response
when Net::HTTPSuccess
user_info = JSON.parse(@response.body)
unless user_info['screen_name']
flash[:notice] = "Authentication failed"
redirect_to :action =>:index
return
end
# We have an authorized user, save the information to the database.
@user = User.new({ :screen_name => user_info['screen_name'],:token => @access_token.token,:secret => @access_token.secret })
@user.save!
# Redirect to the show page
redirect_to(@user)
else
RAILS_DEFAULT_LOGGER.error "Failed to get user info via OAuth"
# The user might have rejected this application. Or there was some other error during the request.
flash[:notice] = "Authentication failed"
redirect_to :action => :index
return
end
end

end
def show
@user = User.find(params[:id])
# Get this users favorites via OAuth
@access_token = OAuth::AccessToken.new(UsersController.consumer, @user.token, @user.secret)
RAILS_DEFAULT_LOGGER.error "Making OAuth request for #{@user.inspect} with #{@access_token.inspect}"
@response = UsersController.consumer.request(:get, '/favorites.json', @access_token,
{ :scheme => :query_string })
case @response
when Net::HTTPSuccess
@favorites = JSON.parse(@response.body)
respond_to do |format|
format.html # show.html.erb
end
else
RAILS_DEFAULT_LOGGER.error "Failed to get favorites via OAuth for #{@user}"
# The user might have rejected this application. Or there was some other error during the request.
flash[:notice] = "Authentication failed"
redirect_to :action => :index
return
end
end

Add below code in /users/show.html.erb

<p>
<b>Screen name:</b>
<%=h @user.screen_name%>
<p>
<ul>
<% @favorites.each do |fav| %>
<li><b><%= fav['user']['screen_name']  ></b><%=h fav['text']%></li>
<%end%>
</ul>
<%= link_to 'Back', users_path%>

/users/new.html.erb

<h1>New user</h1>
<% form_for(@user) do |f| %>
<%= f.error_messages %>
<p>
We'll be sending you to Twitter in a moment to login and grant us access.
Once you allow us in we'll give you the super-cool feature
you'bve heard about.
</p>
<p>
<%= f.submit "Grant Access" %>
</p>
<% end %>
<%= link_to 'Back', users_path %>

route.rb

map.connect '/callback', :controller => 'users', :action => 'callback'

Now you have call users/new url and you will be able to login via Twitter account.

Upgrade Instant rails to 2.3

By , June 5, 2009 12:39 am

Instant rails is a very good package for the new ruby on rails (ror) users. This package provide ruby, rails , mysql and apache with pre configure and ready to run, so user  has to only install the Instant rails and start to build the application.

Instant rails is not upgraded from the long time. It is using rails 2.0 and ruby 1.8.6. So we need to upgrade it manually. There are the few steps which need to follow to upgrade it.

Step 1:

 gem install rails -v 2.3

Step 2.

 gem update --system

if above command did not work then try below command

Step 2.1

 gem install rubygems-update

Step 2.2

 update_rubygems

Then check version of rails and ruby

rails -v

You will get 2.3

 ruby -v 

You will get 1.9.2

Panorama Theme by Themocracy