Proper API doc for rails erb objects?

Hi,

I recently started working with rails and maybe Im a bit spoiled coming
from a Java background but is there a proper api document for the
objects that are used in erb.html pages?

For example, I read a tutorial mentioning a form variable that just seem
to magically exist (no idea who/where it is actually instantiated, e.g.
<%= form.label :name, “Name:” %>) but what methods are available in this
form object?

And what other objects, and methods, are magically available on erb.html
pages?

I checked out http://api.rubyonrails.org and it does list a whole bunch
of classes and methods but I have no idea what type this “form” variable
is referencing.

On Tue, Jul 14, 2009 at 1:50 AM, Shahin K. <
[email protected]> wrote:

form object?

And what other objects, and methods, are magically available on erb.html
pages?

I checked out http://api.rubyonrails.org and it does list a whole bunch
of classes and methods but I have no idea what type this “form” variable
is referencing.

Shahin. learning anything takes time. For example. the form in

<%= form.label :name, “Name:” %>

is called block parameter as in the following:

<% form_for :person, :url => { :action => “update” } do |form| %>
<%= f.error_messages %>
First name: <%= form.text_field :first_name %>

Last name : <%= form.text_field :last_name %>

Biography : <%= form.text_area :biography %>

Admin? : <%= form.check_box :admin %>

<% end %>

On Tue, Jul 14, 2009 at 2:00 AM, Conrad T. [email protected]
wrote:

For example, I read a tutorial mentioning a form variable that just seem

Last name : <%= form.text_field :last_name %><br />
Biography : <%= form.text_area :biography %><br />
Admin?    : <%= form.check_box :admin %><br />

<% end %>

Also, I would recommend getting a copy of “Agile Web D. with
Rails
3rd edition” by Dave T. et al. Furthermore, I would take some time
learning about the Rails API.

Good luck,

-Conrad

Conrad T. wrote:

On Tue, Jul 14, 2009 at 2:00 AM, Conrad T. [email protected]
wrote:

For example, I read a tutorial mentioning a form variable that just seem

Last name : <%= form.text_field :last_name %><br />
Biography : <%= form.text_area :biography %><br />
Admin?    : <%= form.check_box :admin %><br />

<% end %>

Also, I would recommend getting a copy of “Agile Web D. with
Rails
3rd edition” by Dave T. et al. Furthermore, I would take some time
learning about the Rails API.

Good luck,

-Conrad

Thanks for the info and I got that book you mentioned. However I wanted
an easily available api doc (like Suns javadoc) where I could find this
“form” parameter (and others like it) and what fields are available. For
example you mention form.text_field, form.text_area. What others are
there and are they listed anywhere?

Just as a mention, erb is not rails specific. It is something that
rails uses heavily in its views templates but it is really ruby
specific.

The Ruby language created the concept of erb so that html files could
house ruby code. If you want to learn more about erb, you need to
search the proper api documentation:

http://ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB.html

The API ref at api.rubyonrails.org is the main source, but reading it
sometimes requires a little more understanding.

The object passed to form_for’s block is an instance of
ActionView::Helpers::FormBuilder. There’s not explicit documentation
for it, as most of it’s methods are metaprogrammed versions of the
methods in ActionView::Helpers::FormHelper. FormBuilder’s methods are
essentially partially applied versions of FormHelper’s, in that they
don’t require the first argument. Thus, the middle statement here:

<% form_for ‘thing’ do |form| %>
<%= form.text_field :name %>
<% end %>

is identical to:

<%= text_field ‘thing’, :name %>

It’s a handy shorthand, and also makes some things (rendering fields
in a partial, for instance) much easier.

Going forward, there are a couple things that will help you figure out
what’s going on:

  • object.inspect will typically get you a short string representation
    of an object; some objects may not show all fields here, but at least
    the type will be visible

  • object.debug (in a view) will output a YAML version of the given
    object, wrapped in ‘pre’ tags. This can be useful for debugging
    models, for instance.

  • if all else fails, install ruby-debug and put ‘debugger’ wherever
    needed. You can even use it inside of ERB views!

Hope this helps.

–Matt J.

On Jul 14, 5:25 am, Shahin K. [email protected]

I’ve been finding it a little annoying as well. I’m learning Rails
through “Agile Web D. with Rails
3rd edition” and I’m also picking up Ruby at the same time. This is
my first web framework…and for the most part, Agile has been a great
source for learning Rails. I’m in Chapter 9, where in the section
where it’s going through partial templates.

<%= render(:partial => “cart_item” , :collection => @cart.items) %>

This is a bit of code from it, where the book uses the method rendor()
(I’ve been following along and writing code as I go). My question,
here, is where do I find documentation for the method render?

I looked it up in the http://api.rubyonrails.org/ api, but when it
comes to render, all it says is " render(template, local_assigns)
" … okay, template I can get, but local_assigns? What are they? I
search, but can’t find anything within the api about it.

Either I’m just too newbie like to get it, or there’s something wrong
with the rails documentation. I’d probably be closer to betting on my
newbieness :stuck_out_tongue:

Thanks guys,

On Jul 14, 12:41 pm, “Älphä Blüë” [email protected]

Matt J. wrote:

The API ref at api.rubyonrails.org is the main source, but reading it
sometimes requires a little more understanding.

The object passed to form_for’s block is an instance of
ActionView::Helpers::FormBuilder. There’s not explicit documentation
for it, as most of it’s methods are metaprogrammed versions of the
methods in ActionView::Helpers::FormHelper. FormBuilder’s methods are
essentially partially applied versions of FormHelper’s, in that they
don’t require the first argument. Thus, the middle statement here:

<% form_for ‘thing’ do |form| %>
<%= form.text_field :name %>
<% end %>

is identical to:

<%= text_field ‘thing’, :name %>

It’s a handy shorthand, and also makes some things (rendering fields
in a partial, for instance) much easier.

Going forward, there are a couple things that will help you figure out
what’s going on:

  • object.inspect will typically get you a short string representation
    of an object; some objects may not show all fields here, but at least
    the type will be visible

  • object.debug (in a view) will output a YAML version of the given
    object, wrapped in ‘pre’ tags. This can be useful for debugging
    models, for instance.

  • if all else fails, install ruby-debug and put ‘debugger’ wherever
    needed. You can even use it inside of ERB views!

Hope this helps.

–Matt J.

On Jul 14, 5:25�am, Shahin K. [email protected]

Thanks, that will help alot.

However as the poster above mentions there is alot to be desired when it
comes to Ruby/Rails documentation. Like the render method, I too am
wondering what local_assigns is and how they can be used.

From the name I assume it is a way to pass local variables to the page
but that is only an assumption on my part.

Others issues I have is with the ORM framework and the find method. It
can take a whole bunch of options (like :first and so on) but where are
all these options listed?

Shahin K. wrote:
[…]

However as the poster above mentions there is alot to be desired when it
comes to Ruby/Rails documentation.

The documentation is actually very good, but you have to understand how
it’s organized.

Like the render method, I too am
wondering what local_assigns is and how they can be used.

From the name I assume it is a way to pass local variables to the page
but that is only an assumption on my part.

This is addressed somewhere in the RDoc, probably in ActionController,
although I don’t remember offhand.

A lot of useful information is in the preamble to each class document,
rather than under individual methods. This makes sense when you
consider that many of the most commonly used methods in the Rails
framework are actually metaprogrammed in or faked with method_missing,
so RDoc won’t create entries for them.

Others issues I have is with the ORM framework and the find method. It
can take a whole bunch of options (like :first and so on) but where are
all these options listed?

Right in the ActiveRecord::Base RDoc file. Try reading it sometime. :slight_smile:

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]