【问题标题】:page.insert_html not rendering partial correctlypage.insert_html 未正确呈现部分
【发布时间】:2010-05-26 23:19:48
【问题描述】:

以下内容在 text_field 中。

= f.text_field :title, :size => 50, :onchange => remote_function(:update => :suggestions, :url => {:action => :display_question_search_results})

以下内容在 display_questions_search_results.rjs 中。

page.insert_html :bottom, 'suggestions', :partial => 'suggestions'

每当用户键入时,我都想在数据库中搜索与文本字段中的关键字匹配的任何元组。然后,显示这些结果。但是,目前,_suggestions.haml 只包含“suggestions!!”这个词。

但是,而不是看到“建议!!”在suggestions div 标签中,我得到:

try { Element.insert("suggestions", { bottom: "suggestions!!" }); } catch (e) { alert('RJS error:\n\n' + e.toString()); alert('Element.insert(\"suggestions\", { bottom: \"suggestions!!\" });'); throw e }

我一直试图找出这样做的原因,但我发现之前提出的问题似乎比我正在做的更复杂......

【问题讨论】:

    标签: ruby-on-rails partial-views rjs


    【解决方案1】:

    如果您指定要更新的元素,则假定您的操作将返回 html(而不是 rjs/javascript),它将替换指定元素的内容。

    所以,如果您需要替换 :suggestions 的内容,只需在控制器中渲染部分内容:

    render :partial => 'suggestions'
    

    但如果您需要向此元素添加内容,而不是替换,请从您的视图代码中删除:update => :suggestions

    = f.text_field :title, :size => 50, :onchange => remote_function(:url => {:action => :display_question_search_results})
    

    保持控制器代码不变:

    page.insert_html :bottom, 'suggestions', :partial => 'suggestions'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-15
      • 1970-01-01
      • 2018-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多