Partial views and forms/ design patterns

Consider a team of players.

I would like to make my _form not include the form header. This would
allow
me to have different fields available when depending on whether I am
editing or creating a new player. It also allows me reuse that form as a
partial when building a larger editor page that could say edit 2 players
at
one.

players/_form

<%= f.label :name %> <%= f.text_field :name =>
<%= f.label :number %> <%= f.text_field :number =>

players/new.html.erb

Creating Player

<%= form_for(@player) do |f| %> <%= render 'form' %>
<%= f.label :team %> <%= f.text_field :team =>
<%= f.submit %> <% end %>

players/edit.html.erb

Editing Player

<%= form_for(@player) do |f| %> <%= render 'form' %> <%= f.submit %> <% end %>

team/edit_roster.html.erb

Editing Roster

<%= form_for(@roster) do |f| %> <%= render 'players/form', @roster.first_player %> <%= render 'players/form', @roster.second_player %> <%= f.submit %> <% end %>

I use this pattern all the time with MS-MVC/EF/C#. Is this bad practice
in
rails? The syntax I have given doesn’t quite work, so I guess if I go
with
this, I’ll have to change the new/edit/_form for each one I want to do
this
with. Even the generate _form won’t work, as I don’t have access to |f|,
in
my partial.

What is the ‘railsy’ way of accomplishing this?

Thanks,
~S

Hey S,

You can always pass your form variable |f| to the partial when you
render
it.

i.e.

<%= render partial => ‘line_item’, :locals => {:new_f => f} %>

  • Ryan

Thanks, yeah I suspected there would be a way to pass the form variable
in.

If no one finds my tactic offensive, I guess I’ll go the route.

~S

Thanks. Yeah I figured I could likely pass in the form variable somehow.

I guess if no one finds my tactic offensive, I’ll go the route.

~S

Is there a way to change what gets generated by default?