cadlac
cadlac

Reputation: 3158

Dynamic Partials

I am trying to make a form to create a new product. At the top of the page, I want one to be able to choose what type of product it is, and one that is done a form will populate below with the correct fields (and at any point, one could change the product type)

My assumption is that I should use jQuery and AJAX to render a different partial for each one - that is, if the user selects a product type, then the corresponding form loads a partial that has the appropriate form without a page refresh.

Like in my previous posts, my first question is just to make sure this is acceptable in the rails community.

However, ironically my problem is not with the AJAX or the jQuery but the partial. I created a partial for "shirt" titled "_shirt.html.erb" that looks like below:

<% form_for(:shirt, :url => { :action => 'save_to_session' } ) do |f| %>
<table summary="Shirt form fields">
<tr>    
    <th>Shirt Size S:</th>
</tr>
<tr>
    <td><%= f.select(:size_s, 0..99) %> </td>
</tr>
</table>

  <%= submit_tag("Create Item") %>  
<% end %>

and in my view, I am just trying to render the partial with

<%= render "shirt" %>

but when the page loads, it does not load the form. The body is empty except for a link from the layout. I looked online and could not find what could be wrong with this, any ideas?

Upvotes: 0

Views: 102

Answers (2)

Saran
Saran

Reputation: 713

If you want to render a partial, don't you have to specify the :partial keyword?

<%= render :partial => "shirt" %>

Upvotes: 0

Jay Truluck
Jay Truluck

Reputation: 1519

Rendering a form this way is no necessarily "conventional" but if your application requires this functionality then I would not let rails hold you to a certain functionality. In my opinion you should of course follow rails convention as much as possible but should not let that be a limiting factor.

As far as the form not rendering I believe you just have to add a = to the form tag so it should be:

<%= form_for(:shirt, :url => { :action => 'save_to_session' } ) do |f| %>

instead of

<% form_for(:shirt, :url => { :action => 'save_to_session' } ) do |f| %>

Hope this helps!

Upvotes: 1

Related Questions