dimanche 29 mars 2015

Rails accessing controller actions in a nested attribute form

I have a nested form as follows for Membership with a nested MembershipInvite:



<%= form_for [@user, @car, @membership] do |f| %>
<%= f.hidden_field :car_id, class: "form-control", value: @car.id %>
<%= f.fields_for :membership_invites do |builder| %>
<% @code = Digest::SHA1.hexdigest([Time.now, rand].join) %>
<%= builder.hidden_field :car_id, class: "form-control", value: @car.id %>
<%= builder.hidden_field :invite_code, class: "form-control", value: @code %>
<div class="form-group">
<%= builder.label :user_email %>
<%= builder.text_field :user_email, class: "form-control" %>
</div>
<% end %>
<%= f.submit "Add Member", class: "btn btn-large btn-primary" %>
<% end %>


It works fine and everything is saved, however, I have a deliver call for an email in my MembershipInvite controller (create method) which is not calling. I only want to call this deliver method if something is true in the form.


How can I access the nested form attributes on the Membership controller (as this create methid seems to be calling). Is it along the lines of @membership.membership_invites_attributes?


Memberships Controller



def create

@membership = Membership.create(membership_params)

#I WOULD LIVE TO SEND AN EMAIL HERE DEPENDING ON FIELDS FILLED IN FROM THE MEMBERSHIP FORM AND NESTED MEMBERSHIP INVITES FORM.

if @membership.save
redirect_to current_user, notice: "Membership created"
else
render 'new'
end
end

Aucun commentaire:

Enregistrer un commentaire