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.
Also, I would recommend getting a copy of “Agile Web Development with Rails 3rd edition” by Dave Thomas et al. Furthermore, I would take some time learning about the Rails API.
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!
I've been finding it a little annoying as well. I'm learning Rails
through "Agile Web Development 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.
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