On Mon, Sep 22, 2008 at 3:52 PM, Becca Girl
<[EMAIL PROTECTED]> wrote:
>
> Hi.
>
> I've got a form that I'm using with polymorphic associations.  It's
> super basic.  The form fields will be used in multiple places, but the
> hidden polymorphic fields will change with each model that needs them.
>
> Currently in views/books/new.html.erb
> <% form_for(@review) do |f| %>
>  <b>Title</b><%= f.text_field :title %>
>  <b>Body</b><%= f.text_area :body %>
>  <%= f.hidden_field :reviewable_type, :value => 'Book' %>
>  <%= f.hidden_field :reviewable_id, :value => @book.id %>
>  <%= f.submit "Submit" %>
> <% end %>
>
>
> Currently in views/reviews/_form.rhtml
> <% form_for(@review) do |f| %>
>  <b>Title</b><%= f.text_field :title %>
>  <b>Body</b><%= f.text_area :body %>
>  <%= f.submit "Update" %>
> <% end %>
>
>
> Is it better to just have 2 fields in a form partial in views/reviews
> and then have the other required fields in the appropriate views?
>
> If I DRY them up, they would look like this.
>
>
> In views/books/new.html.erb
> <% form_for(@review) do |f| %>
>
>  <%= render :partial => 'reviews/form', :locals=>{:f => f}  %>
>
>  <%= f.hidden_field :reviewable_type, :value => 'Book' %>
>  <%= f.hidden_field :reviewable_id, :value => @book.id %>
>  <%= f.submit "Submit" %>
> <% end %>
>
>
> In views/reviews/_form.rhtml
> <b>Title</b><%= f.text_field :title %>
> <b>Body</b><%= f.text_area :body %>
>
>
> Thanks for the great guidance!

Becca,

It looks like you're on the right track, but you might want to
familiarize yourself with nested resources/routes in Ruby on Rails.
You shouldn't need to specify the type/id as hidden fields like this.

In your routes, you can do something like:

  map.resources :books, :has_many => [ ::reviews ]

Then where you want to show the review form, you could do something like...

<% form_for :book, :url => book_reviews_path( @book ) do |f| %>

When the form submits, you should see the book_id in the params hash,
which you can use to connect the dots.

Good luck!

Cheers,
Robby



-- 
Robby Russell
Chief Evangelist, Partner

PLANET ARGON, LLC
design // development // hosting

http://www.planetargon.com/
http://www.robbyonrails.com/
aim: planetargon

+1 503 445 2457
+1 877 55 ARGON [toll free]
+1 815 642 4068 [fax]

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to