Kim
Kim

Reputation: 2176

ruby on rails : observe field to display rest of form

I would be grateful if someone could help me out with this.

In my RoR application, i have a form where based on the value entered in a field, the rest of the form fields are displayed accordingly. Please find my code below

Form Code:

<p> Location: <%= f.text_field :LOC %></p>


<% f.fields_for :detail do |builder| %>
<p> Type: <%= builder.select(:DET_TYPE, [['Type A', 'Type A'],
                                         ['Type B', 'Type B'],
                                        ],{ :prompt => "Please select"}
                                        ) %>
</p>

<%= observe_field("sub_detail_DET_TYPE", :frequency => 1,
:url => { :controller => 'references', :action => :display_prod_method },
:with => "'id='+value") %>

<div id="div1"></div>

<% end %>

In my references controller:

def display_prod_method
  @type = (params[:id])
end

display_prod_method.rjs:

page.replace_html "div1", :partial => "prodMethod"

_prodMethod.html.erb partial:

<% if @type == "Type A" %>
<p>Production Method: <%= builder.select(:PROD_METHOD, [['Method A', 'Method A'],
                                                  ['Method B', 'Method B'],
                                                  ],{ :prompt => "Please select"}
                                                  ) %>
</p>
 <% end %>

The thing is that if there is only text in the partial, it is rendered in my main form when i select 'Type A'. However when i put the code below in the partial, it is not displayed at all -

<%= builder.select(:PROD_METHOD, [['Method A', 'Method A'],
                                                      ['Method B', 'Method B'],
                                                      ],{ :prompt => "Please select"}
                                                      ) %>

Please can someone shed some light on this for me.

Thanks a lot for your help :)

Upvotes: 2

Views: 691

Answers (1)

sled
sled

Reputation: 14635

that's because you haven't access to a form builder in your partial. Because there is no form_for around it.

So you should go for the static tag function.

Put this in your partial:

<%= select_tag :prod_method, [['Method A', 'Method A'],['Method B', 'Method B']], { :prompt => "Please select"} %>  

Spend attention on the naming, maybe you have to adjust this because it's encapsulated in fields_for.

Edit:

Actually your attempt should produce an error, check your rails server console for that because the error won't be visible in your browser because of the AJAX call.

Upvotes: 1

Related Questions