Stephan Walter
2005-Nov-03 21:16 UTC
a newbie''s questions after reading the Onlamp tutorial
I read the tutorial "Rolling with Ruby on Rails" [1] by Curt Hibbs.
After having followed all the steps, I found some ways to make the code
simpler. First, I replaced the form tags like <input> with text_field,
text_area etc. Then I found out that there is a general "form"
function.
In order to implement categories for items, I extended the form like this:
(in app/views/recipe/edit.rhtml)
<%= form(''recipe'') do |form|
form << content_tag("p",
[content_tag("label", ''Category'',
''for'' => ''recipe_category_id''),
tag("br"),
select(''recipe'', "category_id",
Category.find_all.collect {|c| [
c.name, c.id ] })])
end %>
It works, but would you experts consider it good style? Is there a
simpler way to do this? Especially I''d rather not write the
"label"
stuff manually, because Rails already does this with the other fields of
the recipe.
I read "picking up where ruby on rails'' scaffolding leaves
you" [2] on
technoweenie, and then tried this:
<%= form(''recipe'') do |form|
form.add_field Category:id
end %>
which didn''t work (Did I mention I''m a total Ruby newbie?...
newbie on
nails :-)
Another related question: should I use "scaffold :foo" and try to hack
stuff into that or rather run the scaffolding script and use the
generated code?
-Stephan
[1] http://www.onlamp.com/pub/a/onlamp/2005/01/20/rails.html?page=1
[2]
http://techno-weenie.net/blog/code/255/picking-up-where-ruby-on-rails-scaffolding-leaves-you