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
On Tuesday, 19 June 2012 17:39:19 UTC-6, Ryan Buckley wrote:
>
> 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
>
> On Tuesday, June 19, 2012 1:29:06 PM UTC-10, sheamus wrote:
>>
>> 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
>> <div>
>> <%= f.label :name %>
>> <%= f.text_field :name =>
>> </div>
>> <div>
>> <%= f.label :number %>
>> <%= f.text_field :number =>
>> </div>
>>
>> players/new.html.erb
>> <h1>Creating Player</h1>
>> <%= form_for(@player) do |f| %>
>> <%= render 'form' %>
>> <div>
>> <%= f.label :team %>
>> <%= f.text_field :team =>
>> </div>
>> <%= f.submit %>
>> <% end %>
>>
>> players/edit.html.erb
>> <h1>Editing Player</h1>
>> <%= form_for(@player) do |f| %>
>> <%= render 'form' %>
>> <!--
>> can't change teams once you have been created.
>> <div>
>> <%= f.label :team %>
>> <%= f.text_field :team =>
>> </div>
>> -->
>> <%= f.submit %>
>> <% end %>
>>
>> team/edit_roster.html.erb
>> <h1>Editing Roster</h1>
>> <%= form_for(@roster) do |f| %>
>> <!-- edit the TwoPlayers object which contains two players -->
>> <%= 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
>>
>>
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/rubyonrails-talk/-/Dp8JiQMD24EJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.