Ruby on Rails

Passing optional local variables to a partial

In verbose_index.html.erb i want to pass some additional parameters to the _thing.html.erb partial:

1
2
3
4
<% @things.each do |thing| %>
<%= render :partial => thing,
  :locals => {:some_extra_stuff => some_extra_stuff} %>
<% end %>

in index.html.erb i do not want to pass any additional parameters to the _thing.html.erb partial:

1
<%= render :partial => things %>

when I hit verbose_index everything will work fine.  but when I hit index, I’ll see this error:

1
2
undefined local variable or method 'some_extra_stuff'
  for #<ActionView::Base:0x22e6948>

to fix, in _thing.html.erb I add a check to see if the local variable is assigned:

1
2
3
<% unless local_assigns[:some_extra_stuff].nil?  %>
<%= some_extra_stuff %>
<% end %>

Reader Discussion

Comment on this post




Please enter the word you see in the image below:


Related posts

strftime without leading zeros (0s)

In Rails I wanted to display times like 2:30 am without having a leading zero like 02:30 am.

The view code that gave leading zeros looked like this:

1
<%= foo.updated_at.strftime("%I:%M %p") %>

The code that will trim leading zeros looks like this:

1
<%= foo.updated_at.to_datetime.strftime("%-1I:%M %p") %>

The trick is to convert the time object from ActiveSupport::TimeWithZone to DateTime, which supports prefix modifiers like ‘-1’ in the format expression.

read more

Validate One ActiveRecord Attribute

I am using restful_authentication and want to extend it with functionality to invite a person using only his email.  So, I wanted the ability to validate just one attribute of a model.  Expressia::Blog described the same problem here and had built the plugin I wanted. Only all the links to the plugin pointed back to the original post.  I eventually found the plugin install and usage documentation here at RailsLodge.

1
ruby script/plugin install svn://vinsol.com/plugins/sur/validate_attributes

Now, my relevant code looks like this:

1
@person.validate_attributes(:only => ['email'])

read more

Installing restful_authentication and acts_as_state_machine (aasm) as plugins

Here are the steps I took to install restful_authentication with acts_as_state_machine (using the new aasm gem) as plugins.

Started in the rails home directory of the app. 

Installed aasm gem as a plugin:

1
script/plugin install git://github.com/rubyist/aasm.git

(thanks to Erik on Rails for the above)

Installed restful authentication:

1
2
3
git submodule add \
  git://github.com/technoweenie/restful-authentication.git \
  vendor/plugins/restful_authentication

I use capistrano so I also added the following to my config/deploy.rb file

1
set :git_enable_submodules, 1

Created a lib directory in the app home directory:

1
mkdir lib

Called the restful authentication generator.  Fyi, instead of user, I use person as the name of my model for people:

1
./script/generate authenticated person sessions --include-activation --aasm

Then I did the post-generate steps, following some of the instructions at github.  Some of the steps enumerated in the docs seem to be taken care of as part of the generate call now.

In config/routes.rb, I did a find/replace on peoples (incorrect plural) replacing all with people (correct plural) and added the activate route.  In the end my routes.rb looked like this:

1
2
3
4
5
6
7
8
9
map.logout '/logout', :controller => 'sessions', :action => 'destroy'
map.login '/login', :controller => 'sessions', :action => 'new'
map.register '/register', :controller => 'people', :action => 'create'
map.signup '/signup', :controller => 'people', :action => 'new'
map.activate '/activate/:activation_code', :controller => 'people',
                    :action => 'activate', :activation_code => nil
map.resources :people, :member => { :suspend   => :put,
                                   :unsuspend => :put,
                                   :purge     => :delete }

In config/environment.rb, I added:

1
config.active_record.observers = :person_observer

So I can use before filters in my controllers, I modified app/controllers/application.rb to include AuthenticatedSystem:

1
2
3
4
class ApplicationController < ActionController::Base
  helper :all
  include AuthenticatedSystem
end

I adjusted the language in app/models/person_mailer.rb, especially replacing the YOURSITE references to be the URL of my site:

1
2
@body[:url]  = 
    "http://www.landlessness.net/activate/#{person.activation_code}"

read more