Reputation: 985
I have 2 controllers: Welcome (which handles the homepage) and Invitations (Which help people to create/manage invites). I am trying to have on the same welcome.html.erb
both partials from Invitation view: _form.html.erb
(for creating invitation) and _results.html.erb
(to display the invites via Ajax on the same welcome.html.erb
page).
I also have have create.js.erb
in the Invitation's view folder.
The input work well but there is no live update. I still need to refresh the page. Does anyone know what i am doing wrong please?
_form.html.erb
code is:
<h3 class="section_title"> Express meeting request IR </h3>
<div id="theform">
<%= simple_form_for invitation do |f| %>
<%= f.error_notification %>
<%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>
<%= f.label "Dest." %>
<%= f.collection_select(:guest_id, User.all, :id, :full_name, {}, class: "selectpicker", title: "Choose recipient", multiple: true, data: {style: "form-control", size: "200", width: "100%"}) %>
<br><br>
<%= f.input :event_id %>
<%= f.input :start %>
<%= f.input :title %>
<%= f.input :type_of_event %>
<%= f.label "Memo" %> <br>
<%= f.text_area :memo, as: :text, class: "text_area_iro", rows: 5 %>
<div class="form-actions">
<%= f.button :submit, class: "btn-custom"%>
</div>
<% end %>
</div>
_result.html.erb
(certain header of the table removed) looks as:
<% @invitationsR.each do |invitation| %>
<tr>
<td><%= find_name(invitation.user_id) %></td>
<td><%= invitation.start %></td>
<td><%= TypeEvenement.find_by(id: invitation.type_of_event).type_name %> </td> </tr>
<% end %>
create.js.erb
looks as:
$("#theform").html("<%= escape_javascript(render :partial => 'invitations/result', :locals => {:invitation => @invitation}) %>");
Welcome.html.erb
looks like
<%= render :partial => 'invitations/form', :locals => {:invitation => Invitation.new} %>
<%= render :partial => 'invitations/result', :locals => {:invitations => @invitations} %>
The controller is as usual with plain new and create methods. The create method is as follows:
def create
@listmail = params[:invitation][:guest_id]
@listmail = @listmail.join(' ').split
@listmail.each do |v|
@invitation = current_user.invitations.build(invitation_params)
@invitation.guest_id = v
puts(v)
@invitation.save!
end
if @invitation.save
redirect_to invitations_path
else
render 'new'
end
end
Upvotes: 3
Views: 91
Reputation: 985
The following code works to clear the form after the Ajax validation:
$("#result_partial").html("<%= j render partial: 'invitations/result', :locals => {:invitation => @invitation} %>");
$("#result2_partial").html("<%= j render partial: 'invitations/result2', :locals => {:invitation => @invitation} %>");
$("#theform").remove();
$('#invitation_form').html("<%= j render partial: 'invitations/form', :locals => {:invitation => Invitation.new} %>");
Just do not forget to reset the select2 items with :
$(document).ready(function(){$('#invitation_guest_id').select2({
placeholder: "Contact name, list or company",
minimumInputLength: 3
});
});
Upvotes: 1
Reputation: 6531
1 => Add remote :remote=> true
to let form submit via ajax request.
<%= simple_form_for invitation, id: "invitation_form", :remote=> true do |f| %>
###Content
<%end%>
2 => wrap result partial within a separate id as: -
<%= render :partial => 'invitations/form', :locals => {:invitation => Invitation.new} %>
<div id "result_partial">
<%= render :partial => 'invitations/result', :locals => {:invitations => @invitations} %>
</div>
3 => It's always good practice to use local variable (invitations instead of @invitations) in partial i.e
In _result.html.erb
<% invitations.each do |invitation| %>
<tr>
<td><%= find_name(invitation.user_id) %></td>
<td><%= invitation.start %></td>
<td><%= TypeEvenement.find_by(id: invitation.type_of_event).type_name %> </td>
</tr>
<% end %>
4 => Refactor create action (i.e always use in which format you should respond in html or js)
def create
@listmail = params[:invitation][:guest_id]
@listmail = @listmail.join(' ').split
@listmail.each do |v|
@invitation = current_user.invitations.build(invitation_params)
@invitation.guest_id = v
puts(v)
if @invitation.save!
@invitations = current_user.invitations #get all invitations including new invitations that has been saved just now
respond_to do |format|
format.html{ redirect_to invitations_path }
format.js { render :layout => false }
end
else
window.location = "http://localhost:3000/invitations.new";
end
end
end
5 => After respond back
$('#invitation_form').[0].reset(); #reset form
$("#result_partial").html("<%= j render partial: 'invitations/result', :locals => {:invitation => @invitation}) %>"); #Refresh only results
Upvotes: 2
Reputation: 5895
You have some typos and missing points.
In your create method you need to define a instance variable called @invitations
which you will pass in the partial as a local variable in create.js.erb
file:
def create
---- skip code ----
@invitation.save!
end
@invitations = current_user.invitations # define this one
if @invitation.save
---- skip code ----
end
Now that you define the variable, thus in create.js.erb
file would be:
$("#theform").html("<%= escape_javascript(render :partial => 'invitations/result', :locals => {:invitations => @invitations}) %>");
In _result.html.erb
:
<% invitations.each do |invitation| %>
## Content
<% end %>
Upvotes: 2