inside_layout Broken in Rails 2.1

My good old friend for nested layouts, inside_layout

def inside_layout(layout, &block)
  layout = layout.include?('/') ? layout : "layouts/#{layout}"
  concat(@template.render_file(layout, true,
    '@content_for_layout' => capture(&block)), block.binding)
end

suddenly stopped to work, and since i am so grateful for his long months of service, he got replaced by the next best thing ๐Ÿ˜‰

<% @content_for_layout = capture do %>
...some text or html...
<%= render :file=>'layouts/application', :content_for_layout => @content_for_layout %>
ย 

Simple Tag IO with acts_as_taggable_on_steroids

Update: deleted since it is now redundant

Update: switch to acts_as_taggable_on_steroids more features, less pain/bugs ๐Ÿ™‚
Need simple input/output for tags and got acts_as_taggable_on_steroids ?

…..

and in your forms:

f.text_area('tag_list')

Done!

#Spec:
  it "does not create duplicated tags" do
    @taggable.tag_list = "Bad, Bad, Evil"
    @taggable.save!
    @taggable.tags.size.should == 2
  end

#Often need count of Tags ?
#taggings.rb
belongs_to :tag, :counter_cache=>'taggings_count'

Getting started with acts_as_searchable on Ubuntu

UPDATE

acts_as_searchable is no longer maintained, please switch over to the new and healthy world of search_do bug-free, clear structure and paginating!

Too many crappy tutorials around, with wrong or only partial information of how to get started. Took me almost 2 hours to get it working, so here comes a short How To/Summary for future reference.

  1. sudo apt-get install hyperestraier
  2. sudo nano /etc/default/hyperestraier (change NO_START from 1 to 0) -> autostart on boot
  3. sudo /etc/init.d/hyperestraier start
  4. http://localhost:1978/ -> admin/admin -> create new nodes (xx_test/xx_development/xx_production)
  5. script/plugin install http://github.com/grosser/search_do
  6. add to database.yml (once for every environment):
    estraier:
      host: localhost
      user: admin
      password: admin
      port: 1978
      node: xx_development
  7. >ModelName<.rb: acts_as_searchable :searchable_fields=>[:descr,:title] details
  8. Add all current models to the fulltext search:
    script/console
    >ModelName<.reindex!
  9. Check back in the corresponding node @ http://localhost:1978/ to see if documents have been created
  10. play around with >ModelName<.fulltext_search

Hope this helps ๐Ÿ™‚

more detailed help

Unobtrusive Autocomplete Rails Plugin

Auto complete solution, with customizable results (return just the word or word+id…) and no framework dependency, just use a plain text_field + class=> autocomplete and every autocomplete libary you like (included: jQuery).

autocomplete

# Controller
class UserController < ApplicationController
  autocomplete_for :user, :name
end

#Customized....
autocomplete_for :user, :name do |items,method|
  render :text => items.map{|item| "#{item.send(method)} -- #{item.id}"}.join("\n")
end

# View
<%= f.text_field :auto_user_name, :class => 'autocomplete', 'autocomplete_url'=>autocomplete_for_user_name_users_path %>

# Routes
map.resources :users, :collection => { :autocomplete_for_user_name => :get}

#JS
#any library you like
#(includes examples for jquery jquery.js + jquery.autocomplete.js + jquery.autocomplete.css )
jQuery(function($){//on document ready
  //autocomplete
  $('input.autocomplete').each(function(){
    var input = $(this);
    input.autocomplete(input.attr('autocomplete_url'));
  });
});

#Model(input/output association)
class User
  find_by_autocomplete('name')
end
class Post
  autocomplete_for('user','name') # auto_user_name= + auto_user_name
end
.

Not as thought free as the default version, but gives you a lot more control.

script/plugin install git://github.com/grosser/simple_auto_complete.git
README

Tests should be Documentation with Examples

Since no one wants to write the documentation, the simplest solution would be to let everyone write documentation, without noticing…

Normal documentation:

An Organisation is a firm or school that has an 
address(required) and can have users as members.

Pragmatig documentation with examples:

Organisation:
- requires an address
  organisation.address
- can have users as members
  organisation.members << User

Test:

describe Organisation do
  it "requires an address" do
    @organisation.should_not be_valid?
    @organisation.address = Address.new
    @organisation.should be_valid
  end

  it "can have users as members" do
    @organisation.should be_valid
    @organisation.members << User.new
    @organisation.should be_valid
  end
end
#before :each omitted

With the help of some spec -f or agiledox magic, we can extract:

An Organisation:
- requires an address
- can have users as members

looks familliar ?

I prefer this kind of testing, since it does not generate so many 3-liners(2(do/end)+1) nor uses “it should” all the time, which makes it look more documentation-ish.

DISCLAIMER: This way of writing tests is not RSpec-pure in that it does not use ‘it should’ and has more than on assertion (should) per example.