【问题标题】:best_in_place does not show nil value when no layoutbest_in_place 没有布局时不显示 nil 值
【发布时间】:2013-09-15 02:41:58
【问题描述】:

我正在渲染一个部分并在没有布局的情况下渲染它

format.html { render :layout => false }

所有值都正确显示,并且可以按预期使用 best_in_place 进行编辑。但是,nil 值显示为空,因此无法编辑(即不显示“-”)。尝试专门使用 :nil 选项并没有什么不同。

在渲染布局时,它会正确显示,但是我不能使用布局。 有什么建议?

示例代码: 控制器

def index
....
@user = User.find(params[:user_id])
  respond_to do |format|
    format.html { render :layout => false } 
    format.json { render json: @user }
  end
end

查看

<td class='centered-cell span2'><%= best_in_place @user, :nick_name, :inner_class => 'span1' %></td>

【问题讨论】:

  • 请粘贴一些代码。
  • 不清楚...什么` nil 值显示为空,因此无法编辑(即没有显示“-”)`??
  • 是的,当属性的值为 nil 时,不是通常显示的“-”,而是一个空字符串,并且无法编辑。
  • 我发现其他人也面临同样的问题,就像这个未解决的问题一样:stackoverflow.com/questions/18725948/…
  • 好的,我相信已经找到了答案——nil 显示是在 JavaScript 部分完成的,通过调用 $('.best_in_place').best_in_place() 来调用。由于我页面中的此代码仅在“就绪”JS 中调用,因此在我加载部分日期时未调用它,因此未调用 best_in_place()。在 AJAX 调用后调用 best_in_place 解决了这个问题。谢谢

标签: ruby-on-rails best-in-place


【解决方案1】:

在我的情况下,我使用了 type: :select 但 collection: 是空的

所以我不得不做这样的事情来捕捉它的空选择框

<%= best_in_place [:admin, resource], :select_attr, type: :select, collection: SelectModel.all.map{|sm| [sm.id, sm.name]}.presence||[nil, 'nil'] %>

【讨论】:

    猜你喜欢
    • 2014-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-08
    相关资源
    最近更新 更多