weixin_33725239 2017-07-17 14:53 采纳率: 0%
浏览 13

使用AJAX更改部分

I learn Ajax from some time, which is great by the way, so i'm not really an expert of it. So I need some advice about it.

I'm currently building a group system. I have a tab system to differentiate your own groups and the others. What I trying to do know, is script an ajax to send my partial group from a tab to the other. For example when the user quit a group, it will disappear of my groups to appear in the tab others.

I succeed the first step but I don't really know how I can send the form into the other tab. As you can see bellow, I've try to send the id, but there is no partial form include. So it's just rendering the id of my group. Any advices to include the partial (_group2.html.erb) ?

My code :

Index(group):

<div class="panel-body">
    <div class="tab-content">
      <div class="tab-pane fade in active" id="tab1default_group" style="padding:0px;">
        <div class="group_area">
          <% @my_groups.each do |group| %>
            <div class="group_box<%=group.id%>">
              <%= render 'group', group: group %>
            </div>
          <% end %>
        </div>
      </div>
      <div class="tab-pane fade in" id="tab2default_group" style="padding:0px;">
        <div class="group_area2">
          <% @groups_out.each do |group| %>
            <div class="group_box2<%=group.id%>">
              <%= render 'group2', group: group %>
            </div>
          <% end %>
        </div>
      </div>
    </div>
  </div>

rem_req.js.erb(group):

$(".group_box<%=@group.id%>").fadeOut();
$(".group_area2").append('<%= render partial: @group2 %>');

Groups_controller :

  def rem_req
    if @group.groupes_admin.count == 1 && @group.groupes_admin.where(user_id: current_user).any?
      flash[:error] = "Vous devez définir un nouvel Admin ou supprimer ce groupe"
    else 
      RemReqGroupJob.perform_later(current_user, @group)
      flash[:error] = "Vous ne faites plus parti de #{@group.name}"
      @group2 = Group.where(id: @group.id).last
    end
    respond_to do |format|
      format.html { redirect_back }
      format.js
    end
  end

And when i try to run this, I have this error :

ActionView::Template::Error ('nil' is not an ActiveModel-compatible object. It must implement :to_partial_path.):
  • 写回答

1条回答 默认 最新

  • elliott.david 2017-07-17 18:21
    关注

    I assume the happy case scenario to give you direction. I think you are close but you need to make some tweak

    From the snippet

    <% @groups_out.each do |group| %>
      <div class="group_box2<%=group.id%>">
        <%= render 'group2', group: group %>
      </div>
    <% end %>
    

    first move <div class="group_box2<%=group.id%>">..</div> also in group2 partial.

    Then replace $(".group_area2").append('<%= render partial: @group2 %>'); with $(".group_area2").append('<%= j(render(partial: "group2")) %>');

    评论

报告相同问题?

悬赏问题

  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Bug traq 数据包 大概什么价
  • ¥15 在anaconda上pytorch和paddle paddle下载报错
  • ¥25 自动填写QQ腾讯文档收集表