Archive for the ‘Solutions’ Category

Merb bundler

Merb is increasingly impressive. These guys are amazing. Check out the bundler.

My favorite merb guides, tutorials, and resources

Authenticated Hello World

Basic user Signup with MerbAuth

Datamapper documentation - Unlike Rails you can choose your ORM and unlike Rails the ORM docs are stored outside of the core Merb documentation. This is the link to the documentation of the ORM of my choice.

Difference between render and display in merb

Rails-style flash messages with merb. Merb does it cleaner than Rails by not creating a throwaway session.

Merb controller testing

MerbCamp video
Merbcamp recap(s)

MerbAuth Basics - before :ensure_authenticated, :exclude => :index

Nested Resources/Routes information

Deploying Merb apps

Deloying Merb apps on passenger

Merb subdomains

Here’s how to handle dynamic subdomains in Merb in the router.

How to find cause of heavy usage on your Apache webserver

This is a great little post on “how to find cause of heavy usage on your Apache webserver”.

How to ban bad ips in a rails app

The same is applicable for a Merb app, but there might be a better way to do it. Just put the following your application controller.

Easy ajax sorting with will_paginate and ujs_sort_helper for rails

Install will_paginate

script/plugin install git://github.com/mislav/will_paginate.git

Install ujs_sort_helper

script/plugin install git://github.com/pengwynn/ujs_sort_helper.git

Install livequery

Livequery is just a javascript file. You can grab it from github (copy and paste is fine) and put it under public/javascripts/jquery.livequery.js.

Configure will_paginate and ujs_sort_helper together

Edit your controller’s list action. This example is nesting will_paginate and so is a little more complex than what you’ll probably use, but sometimes it’s rewarding to the reader to see a little bit more complexity. The key text to look at is the ‘options’ clause though.

1
2
3
4
5
6
7
8
9
10
11
12
13
  def show
    sort_init 'created_at'
    sort_update
    options = {:page => params[:page], :per_page => 10, :order => sort_clause }
 
    @project = Project.include_comments.include_watched_projects.find(params[:id])
    @comments = @project.comments.order_by.include_user.include_uploads.include_roles.paginate(options)
 
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @project }
    end
  end

Configure application.rhtml with the necessary javascripts

My example is actually using jquery via the jrails plugin so if you are using prototype this might look a bit different. See the README for info on using prototype instead.

1
2
3
4
	<%= javascript_include_tag :defaults %>
	<%= stylesheet_link_tag "ujs_sort_helper"%>
	<%= javascript_include_tag "jquery.livequery.js" %>
	<%= javascript_include_tag "ujs_sort_helper.jquery.js", :plugin => "ujs_sort_helper"%>

Setup your view

I am not using the typical table so your example might be slightly different, but I figured it might be nice for other people to see how it functions without using a table.

In show.html.erb or list.html.erb or index.html.erb (whatever you used previously in your controller) place your will_paginate links and ujs_sort_helper link(s).

1
2
3
4
5
6
<!-- Comments -->
<div id="comments">	
	<p id="sort_comments" align="right"><%= sort_header_tag('created_at', :caption => 'Order') %></p>
	<%= render :partial => 'comments/comment', :collection => @comments %>
	<%= will_paginate @comments %>
</div>

As you can see, my code uses the sort_header_tag to create the sort link, and I am specifying the text caption with the :caption option. Then I am calling my comment partial with my array of @comments from the controller. And finally, I am including the pagination links using will_paginate and the array I want to paginate.

Edit ujs_sort_helper.jquery.js

We need this file to load the proper div for the ajax sort call.

1
2
3
4
5
6
7
8
9
10
11
12
13
 
 
jQuery(document).ready(function() {
	jQuery('a.sort_link').livequery('click', function(e) {
		jQuery('#comments').load(e.target.href + " div#comments"); 
		return false;
	});
 
	jQuery('div.pagination a').livequery('click', function(e) {
		jQuery('#comments').load(e.target.href + " div#comments"); 
		return false;
	});
});

Merb simple_format helper

Merb does not have a simple_format helper like in Rails so you can do the following.

Add this to your Application Helper

1
2
3
4
5
6
7
8
9
10
11
      def simple_format(text, html_options={})
        text = text.to_s.dup
        text.gsub!(/\r\n?/, "\n")
        text = text.split(/\n\n+/)
        formatted_text = ""
        text.each do |line|
          line.gsub!(/([^\n]\n)(?=[^\n])/, '\1<br />')
          formatted_text << tag(:p, line, html_options) + "\n\n"
        end
        formatted_text
      end

Use in your view
<%= simple_format(@page.body) %>
<%= simple_format(@page.body, :class => ‘hello’) %>

Thanks goes to matteti for enlighting me on the fact that I could start with the rails simple_format helper and configure as necessary.

Remove quoted text from reply emails in rails

the code

and with rough context:

Thanks malkomalko

Setup Paperclip plugin with Amazon S3 to upload videos in rails

This is taken from Clayton Lengel-Zigich. Thanks.

Create a video model.rb

Configure the config/s3.yml file

Configure and install right_aws gem

Then run: sudo rake gems:install & sudo rake gems:unpack

Create a videos controller with the prospective views for the uploading

Configure flash player to display video

1. Download JW FLV MEDIA PLAYER (mediaplayer.zip download link)
2. Copy and paste the player.swf file at public/flash/player.swf
3. Copy and paste the swfobject.js at public/javascript/swfobject.js
4. Configure your application.html.erb file to call swfobject.js in the head

That’s it, you should now be able to upload flv video files and view them in a flash player by going to http://localhost:3000/videos. For instructions on converting quicktimes videos to flash videos on file upload take a look at Jim Neath’s tutorial on converting videos with rails.

Development vs Production mode

It’s a good idea to create a bucket for development and a bucket for production. If you decide to, you can do the following.
Note:this assumes you started your app with bort, and already have a settings.yml that gets loaded by the load_config.rb file using APP_CONFIG = YAML.load_file(”#{RAILS_ROOT}/config/settings.yml”)[RAILS_ENV].symbolize_keys)

In config/settings.yml

In video.rb

Merb: Seeing your routes

Here’s how to see your routes in merb using rake and irb.

It’s similar to rake:routes in rails.

1
rake audit:routes

Or in irb

merb -i
merb.show_routes