Rails 3.1 - Scaffolding Files

13 views
Skip to first unread message

Esteban

unread,
Apr 24, 2012, 8:35:22 PM4/24/12
to rubyonra...@googlegroups.com
Where can I find more information about the new syntax on these:

<%- model_class = @product.class -%>
<h1><%=t '.title', :default => t('helpers.titles.new', :model => model_class.model_name.human,
                               :default => "New #{model_class.model_name.human}") %></h1>
<%= render :partial => 'form' %>

I've been using rails for a little bit and the "model_class = ..." is all new to me, plus I can't make sense of this:

<%=t '.title', :default => t('helpers.titles.new', :model => model_class.model_name.human, :default => "New #{model_class.model_name.human}") %>

Thanks!

Robert Walker

unread,
Apr 26, 2012, 7:57:32 PM4/26/12
to rubyonra...@googlegroups.com
Esteban wrote in post #1058203:
> Where can I find more information about the new syntax on these:
>
> <%- model_class = @product.class -%>

Here the local variable model_class is assigned the value returned by
the instance method class. This would be the constant identifying the
class name of the instance variable @product. "Product' would most
likely be the name of the class returned and stored in model_class.

> I've been using rails for a little bit and the "model_class = ..." is
> all
> new to me, plus I can't make sense of this:
>
> <%=t '.title', :default => t('helpers.titles.new', :model =>

Here you are using the "t" method (a.k.a. "Translate"). It's part of the
Rails Internationalization (I18n).

Read more about the "t" method here:
http://guides.rubyonrails.org/i18n.html#overview-of-the-i18n-api-features

Pay special attention to section 4.1.4 “Lazy” Lookup of the guide.>
model_class.model_name.human, :default => "New

> #{model_class.model_name.human}") %>

Here you are calling the instance method "model_name", which is defined
in ActiveModel::Naming module. Using the method "human", defined in
ActiveModel:Name you get a humanized version of the name of the model
(i.e. "Purchase Order" instead of "PurchaseOrder").

http://api.rubyonrails.org/classes/ActiveModel/Naming.html#method-i-model_name
http://api.rubyonrails.org/classes/ActiveModel/Name.html#method-i-human

--
Posted via http://www.ruby-forum.com/.

Robert Walker

unread,
Apr 26, 2012, 7:59:10 PM4/26/12
to rubyonra...@googlegroups.com
Robert Walker wrote in post #1058555:
> Here the local variable model_class is assigned the value returned by
> the instance method class. This would be the constant identifying the
> class name of the instance variable @product. "Product' would most
> likely be the name of the class returned and stored in model_class.

Sorry, I forgot to go back and fix one minor error in the above. The
"class" method would return you the actual class object not the class
name constant.
Reply all
Reply to author
Forward
0 new messages