Create a nested form.

56 views
Skip to first unread message

Linus Pettersson

unread,
Apr 4, 2012, 2:42:11 PM4/4/12
to rubyonra...@googlegroups.com
Hi!

I have created a nested form and now I want to make it dynamic. I have two models, "Applications" and "Participants". Each application may have several participants, but at least one which is the contact person. An issue that I have is that when the form is not validated it renders the participants fields twice.

- How can I avoid that?

# Applications/new.html.erb
<div class="row span8 offset2 form-wrapper">
<h1>Ny anmälan</h1>

<%= semantic_form_for @application, :html => { :class => "form-horizontal" } do |f| %>
<%= f.semantic_errors %>
<%= f.inputs do %>
<%= f.inputs :for => :participants, :name => "Kontaktperson", :class => "participants_form well" do |p| %>
<%= p.input :name, :label => "Namn", :required => true %>
<%= p.input :address, :label => "Adress", :required => true %>
<%= p.input :phone, :label => "Mobiltelefon", :required => true %>
<%= p.input :email, :label => "Epostadress", :as => :email, :required => true %>
<%= p.input :age, :label => "Ålder" %>
<%= p.input :has_something, :label => "Ja..." %>
<%= p.input :special_food, :label => "Annan specialkost" %>
<%= p.input :is_contact, :as => :hidden, :value => true %>
<% end %>

<%= f.inputs :class => "participants_fields" do %>
<%= render "participant_form", :f => f %>
<% end %>
<%= link_to "<i class=\"icon-plus-sign\"> </i> Lägg till fler deltagare...".html_safe, "#", :class => "btn pull-right" %>

<%= f.inputs :name => "Övrig information" do %>
<%= f.input :extra_information, :input_html => { :class => "span4", :rows => 3 } %>
<%= f.input :membership_paid, :label => "JA" %>
<% end %>
<% end %>

<%= f.buttons do %>
<%= f.submit :value => "Skicka anmälan", :class => "btn btn-primary btn-large commit create" %>
<% end %>
<% end %>
</div>

# Applications Controller
def new
@application = Application.new
@application.participants.build
respond_to do |format|
format.html
format.js { render :nothing }
end
end

def create
@application = Application.new(params[:application])

if @application.save
flash[:notice] = "Din anmälan har skickats!"
redirect_to root_path
else
flash[:error] = "Det gick inte att spara din anmälan"
render :action => "new"
end
end


I guess that my participants gets built again when it renders the new action in the "create" method. But how can I avoid that?

Charles A. Lopez

unread,
Apr 4, 2012, 2:55:43 PM4/4/12
to rubyonra...@googlegroups.com
is there a way to check if the participant field is already created before creating it?  

Is_A and Has_A relationships. Describe the relationship between the forms


--
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/-/ihqbWCmIL0YJ.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.




Linus Pettersson

unread,
Apr 4, 2012, 3:51:41 PM4/4/12
to rubyonra...@googlegroups.com
I don't quite understand what you mean.

Do you understand my problem?

When the user enters the information into the form correctly and it validates everything works great. However, if it is not correctly filled out the "new" action will get re-rendered and displaying the form errors. The issue is that instead of showing 2 Participant "fields groups" it now displays 4, using the code above.



Den onsdagen den 4:e april 2012 kl. 20:55:43 UTC+2 skrev Charles:
is there a way to check if the participant field is already created before creating it?  

Is_A and Has_A relationships. Describe the relationship between the forms
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.

Charles A. Lopez

unread,
Apr 4, 2012, 4:20:44 PM4/4/12
to rubyonra...@googlegroups.com
when your validation fails you're calling "new" again. you should create something called "validate" 

the path created does not provide desired results. Create a different path. 

To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/fl-g8AYGOXMJ.

To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages