Skip to content
floere edited this page Sep 13, 2010 · 1 revision

A quick one

In the view:

user = view_model_for @user
%h1= "You, #{user.full_name}, the user"
%h2 This is how you look as a search result:
= user.render_as :result
%h2 This is how you look as a Vcard:
= user.render_as :vcard

In the view model:
class ViewModels::User < ViewModels::Project
  model_reader :first_name, :last_name
  def full_name
    "#{last_name}, #{first_name}"
  end
end

In the model:
class User < ActiveRecord::Base
end

Also, there are two partials in app/views/view_models/user/, _result.html.haml and _vcard.html.haml that define how the result of user.render_as :result and user.render_as :vcard look. The ViewModel can be accessed inside the view by using the local variable view_model.