簡體   English   中英

在視圖中將ajax從一個部分實現到另一個部分的問題

[英]Issue implementing ajax from one partial to another partial within a view

我有2個控制器:歡迎(處理主頁)和邀請(幫助人們創建/管理邀請)。 我試圖在邀請視圖中使用相同的welcome.html.erb部分: _form.html.erb (用於創建邀請)和_results.html.erb (在同一個welcome.html.erb上通過Ajax顯示邀請)頁)。 我在邀請的視圖文件夾中也有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方法一樣。 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

你有一些錯別字和遺漏點。 在你的create方法中,你需要定義一個名為@invitations的實例變量,你將把它作為create.js.erb文件中的局部變量傳遞給partial:

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 =>添加remote :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 =>在部分ie中使用局部變量(邀請而不是@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

以下代碼用於在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
    });
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM