Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Hi to accomplish what you need to do, there are a few easy steps to follow: 1) In the model classes (Incident and Person classes in your case) make sure you put something like this:</p> <pre><code>class Incident &lt; ActiveRecord::Base has_and_belongs_to_many :people # Note if you want to be able to remove a person from the Incident form (to de-associate) put true in :allow_destroy =&gt; true on the accepts_nested_attributes_for accepts_nested_attributes_for :people, :allow_destroy =&gt; false, :reject_if =&gt; :all_blank validates_associated :people #so you won't be able to save invalid people objects attr_accessible :date_occur, :location, :people_attributes # note the :people_attributes here #do your Incident validations as usual... end class Person &lt; ActiveRecord::Base has_and_belongs_to_many :incidents #the following line it's to allow mass assignment, basically it will allow you to create people from the Incident form attr_accessible :first_name, :last_name, :dob #do your Person validations as usual... end </code></pre> <p>2) In the view side, the easiest way will be modifying the Incident form file (app/view/incidents/_form.html.erb) to allow the user to assign existing and create new people to the Incident:</p> <pre><code># app/view/incidents/_form.html.erb &lt;%= semantic_form_for(@incident) do |f| %&gt; &lt;% if @incident.errors.any? %&gt; &lt;div id="error_explanation"&gt; &lt;h2&gt;&lt;%= pluralize(@incident.errors.count, "error") %&gt; prohibited this incident from being saved:&lt;/h2&gt; &lt;ul&gt; &lt;% @incident.errors.full_messages.each do |msg| %&gt; &lt;li&gt;&lt;%= msg %&gt;&lt;/li&gt; &lt;% end %&gt; &lt;/ul&gt; &lt;/div&gt; &lt;% end %&gt; &lt;div class="field"&gt; &lt;%= f.label :date_occur %&gt;&lt;br /&gt; &lt;%= f.datetime_select :date_occur %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :location %&gt;&lt;br /&gt; &lt;%= f.text_field :location %&gt; &lt;/div&gt; &lt;%= f.input :people, :as =&gt; :select, :collection=&gt;Hash[Person.all.map { |p| [p.first_name + ' - ' + p.last_name, p.id] }] %&gt; &lt;%= f.fields_for :people, Person.new() do |new_person_form| %&gt; &lt;div class="incident-people new-person"&gt; &lt;%= new_person_form.inputs :name=&gt;'Add New person' do %&gt; &lt;%= new_person_form.input :first_name, :label=&gt;'First Name: ' %&gt; &lt;%= new_person_form.input :last_name, :label=&gt;'Last Name: ' %&gt; &lt;%= new_person_form.input :dob, :label=&gt;'Date of birth: ' %&gt; &lt;% end %&gt; &lt;/div&gt; &lt;% end %&gt; &lt;div class="actions"&gt; &lt;%= f.submit %&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre> <p>3) Lastly, you need to modify your update and create methods for the Incident controller as follow:</p> <pre><code># app/controllers/incident_controller.rb def create selected_people = params[:incident][:person_ids].keep_if{ |v| v.present? } params[:incident].delete(:person_ids) @incident = Incident.new(params[:incident]) @incident.people = Person.find( selected_people ) respond_to do |format| if @incident.save format.html { redirect_to @incident, notice: 'Incident was successfully created.' } format.json { render json: @incident, status: :created, location: @incident } else format.html { render action: "new" } format.json { render json: @incident.errors, status: :unprocessable_entity } end end end def update selected_people = params[:incident][:person_ids].keep_if{ |v| v.present? } params[:incident].delete(:person_ids) @incident = Incident.find(params[:id]) @incident.people = Person.find( selected_people ) respond_to do |format| if @incident.update_attributes(params[:incident]) format.html { redirect_to @incident, notice: 'Incident was successfully updated.' } format.json { head :no_content } else format.html { render action: "edit" } format.json { render json: @incident.errors, status: :unprocessable_entity } end end end </code></pre> <p>And that's it, let me know if you need further help. FedeX</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload