在视图中从一个部分到另一个部分实现ajax的问题

时间:2018-10-24 09:49:52

标签: ruby-on-rails

我有2个控制器:欢迎(处理主页)和邀请(帮助人们创建/管理邀请)。我试图在邀请视图中的两个部分上使用相同的welcome.html.erb_form.html.erb(用于创建邀请)和_results.html.erb(通过Ajax在同一welcome.html.erb上显示邀请页)。 邀请的查看文件夹中也有create.js.erb。 输入工作正常,但没有实时更新。我仍然需要刷新页面。有人知道我在做什么错吗?

_form.html.erb代码是:

<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(某些表格标题已删除)如下:

<% @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如下:

$("#theform").html("<%= escape_javascript(render :partial =>  'invitations/result', :locals => {:invitation => @invitation}) %>");

Welcome.html.erb看起来

<%= render :partial =>  'invitations/form', :locals => {:invitation => Invitation.new} %>
<%= render :partial =>  'invitations/result', :locals => {:invitations => @invitations} %>

控制器和普通的new和create方法一样。   创建方法如下:

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

3 个答案:

答案 0 :(得分:2)

您有一些错别字和遗漏之处。 在您的create方法中,您需要定义一个名为@invitations的实例变量,您将在局部变量中将其作为局部变量传递到create.js.erb文件中:

def create
    ---- skip code ----
      @invitation.save!
    end

    @invitations = current_user.invitations # define this one 

    if @invitation.save
    ---- skip code ----

  end

现在您定义了变量,因此在create.js.erb文件中将是:

$("#theform").html("<%= escape_javascript(render :partial =>  'invitations/result', :locals => {:invitations => @invitations}) %>");

_result.html.erb中:

<% invitations.each do |invitation| %>
    ## Content
<% end %>

答案 1 :(得分:2)

1 =>添加远程:remote=> true,以通过ajax请求提交表单。

<%= simple_form_for invitation, id: "invitation_form", :remote=> true do |f| %>
  ###Content
<%end%>

2 =>将结果部分包装在一个单独的ID中,如下所示:-

<%= render :partial =>  'invitations/form', :locals => {:invitation => Invitation.new} %>
<div id "result_partial">
  <%= render :partial =>  'invitations/result', :locals => {:invitations => @invitations} %>
</div>

3 =>始终最好在局部即局部使用局部变量(邀请而不是@invitations)

_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 =>重构创建动作(即始终使用应以html或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 =>回复后

$('#invitation_form').[0].reset(); #reset form
$("#result_partial").html("<%= j render partial: 'invitations/result', :locals => {:invitation => @invitation}) %>"); #Refresh only results

答案 2 :(得分:1)

以下代码可在Ajax验证后清除表单:

$("#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}  %>");

别忘了用以下命令重置select2项:

  $(document).ready(function(){$('#invitation_guest_id').select2({
        placeholder: "Contact name, list or company",
        minimumInputLength: 3
    });
    });