[rspec-users] Problems with form_for and partials

Russell Norris rsl at swimcommunity.org
Sat Oct 20 14:00:52 EDT 2007


David, thanks for that code. I was thinking of something like that myself.

As for the form_for stuff, problem B solved itself when I created the
form builder with its arguments in the correct order, heh. Problem A
is much bigger and harder as the mock form builder i make creates a
wholly different Proc than the form builder in the code. I'm giving up
trying to solve that and just not specing it to expect_render(...). So
I'm gonna consider this problem solved and move on. :)

RSL

On 10/20/07, David Chelimsky <dchelimsky at gmail.com> wrote:
> On 10/20/07, Russell Norris <rsl at swimcommunity.org> wrote:
> > i'm having problem with a form_for situation where i'm trying to DRY
> > out the repeated parts of my forms and put them in common/form and
> > render the form elements via another partial in controller_name/_form.
> > Here's the first form
> >
> > # app/views/common/form
> > <% form_for ... do |f| -%>
> >   <%= render :partial => "params[:controller]/form", :object => f %>
> >   <p>Submit buttons...</p>
> > <% end -%>
> >
> > I'm having problems with stub_render because of the :object => f. I
> > have tried creating a FormBuilder instance to use there as the :object
> > but it doesn't seem to want to match the actual instance there, which
> > has a large amount of additional information due to the template being
> > run at that point.
> >
> > Then in the partial:
> >
> > # app/views/users/_form
> > <p>
> >   <label>Name</label>
> >   <%= f.text_field :user, ... %>
> > </p>
> >
> > I can create a FormBuilder instance here and solve the 'f' problem by
> > stubbing f and returnign the new FormBuilder but then I have to stub
> > the text_field method which is what i'm actually spec'ing.
> >
> > I'd appreciate any suggestions on how to accomplish this without
> > having to repeat the same basic form information and buttons over
> > multiple forms. Something about sacrificing DRYness to achieve BDD
> > seems off. I'd imagine you can have both and I'm just not hitting the
> > mark.
>
> There should not be any conflict between BDD and DRY code. There has
> been some debate about DRY specs, but that's a completely different
> matter.
>
> One way that I've handled this that works well is that I've got a
> shared behaviour for the common elements in the form and I use that in
> all the specs for the forms that use the partial:
>
> http://pastie.caboo.se/109164
>
> This works whether there is a partial being used or not, and I think
> that it makes it easier to refactor things later.
>
> Cheers,
> David
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list