1

以下是在 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!!”。

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

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

我一直在试图找出为什么要这样做,但我发现以前提出的问题似乎比我正在做的更复杂......

4

1 回答 1

2

如果您指定要更新的元素,则假定您的操作将返回 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'
于 2010-05-26T23:56:35.600 回答