weixin_33701251 2013-07-02 17:32 采纳率: 0%
浏览 18

Rails 3的每种方法和Ajax

I'm trying to implement some Ajax in my app. A strange behaviour occurs! It's a daycare application. When you show a specific daycare of a specific date you can add some children. Originally a list of the children of the database is generated and when you click on one of them the page reload and a new child appears in the attendance list of the daycare. It's working fine, i just wanna add some ajax to be more userfriendly !

When you click on child to add him to the daycare, a daycare_item is created ( join table, an id of the child and the id of the daycare ).

I make the changes to make it ajax ready:

  • partial for the list
  • format.js in the daycare_item controller
  • remote true on the link.

It works, no more reload! But the list is updated only when you click a second time on the children list ( the last child added doesn't appears yet ). The js transaction works and if you refresh manually the page, the missing child appears.

I tried few things and here are my results:

In my partial there are

<% @daycare.daycare_items.each do |c| %>
  <li><%= c.child.firstname ></li>
<% end %>

This produce the "lag" effect with one children who is not showing ( until a full refresh )

But if i put a

<%= @daycare.daycare_items.count %>

the code is update in time ! I see nothing strange in the logs. I'm asking why the .each method make a difference?

  • 写回答

1条回答 默认 最新

  • weixin_33720956 2013-07-03 01:07
    关注

    A var was stoping a part of the code to be executed, the var wasn't wrong and doesn't generate an error.

    The elements involved: daycare_items controller

    def create
      @daycare = Daycare.find(params[:daycare_id]) # for Ajax
      @daycare_item = DaycareItem.new(params[:daycare_item])
    ....
    end
    

    the create.js.erb

    $('#children-list').html(" <%=j render partial: 'daycares/daycare', locals: { daycare: @daycare } %> ");
    

    the view daycares#show

    <div id="children-list">
      <%= render @daycare %>
    </div>
    

    the partial ( a part of ) _daycare.html.erb

    <p>counting test:<%= daycare.daycare_items.count %></p>
    
    # here was something like this <%= @waiting.count %> @waiting is define in the daycare controller but not in the daycare_items controller
    <p>
      <% daycare.daycare_items.each do |dci| %>
        <%= dci.enfant.prenom %>
      <% end %>
    </p>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧