【问题标题】:Why after render an partial,@users_shown is nil?为什么在渲染部分后,@users_shown 为零?
【发布时间】:2012-02-28 04:05:29
【问题描述】:

在控制器中,我有@users_shown。在视图中,我使用@users_shown 渲染部分_attendants.html.haml

没关系,但是这个页面上有一个叫做attend_again的按钮,当你点击这个按钮时,它会通过AJAX渲染部分_attendants.html.haml

问题在部分_attendants.html.haml@users_shownnil。我不知道为什么。

文件 _attendants.html.haml 像这样

- unless @users_shown.blank?
  -@users_shown.each do |user|
    user.name,user.id

【问题讨论】:

  • 你能提供更多信息吗?如果可能的话,我想看看 ajax 调用
  • $('#page_attendants').html(' "xxxx/_page_attendants") %>');
  • 实际上没有 ajax。

标签: ruby-on-rails ruby


【解决方案1】:

您需要在渲染 ajax _attendants 部分的控制器操作中设置 @users_shown。这是一个完全独立的请求,需要适当的所有相关数据集。

【讨论】:

  • 嗨,谢谢。这不是 ajax _attendants,只是 _attendants.html.haml
  • 我意识到了。呈现attemptants.html.erb 的控制器需要设置@users_shown,尽管您使用初始页面呈现设置它。如果您不能轻松设置它,您可以将值放在会话中,但我不建议对任何类型的对象这样做。
【解决方案2】:

尝试在局部渲染中添加一个本地,例如:

<%= render :partial => "xxxx/_page_attendants", :locals => { :users_shown => @users_shown } %>

【讨论】:

    猜你喜欢
    • 2010-12-09
    • 2011-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-29
    • 2016-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多