想要使用ajax进行部分更新

时间:2014-02-25 16:39:34

标签: ruby-on-rails ajax

我有一张表格:

<% form_tag({:controller => "/faq", :action => 'search_ajax'}, :update => "help_results", remote: true) do %>

将进入search_ajax操作,该操作应该更新help_results div。该操作应该在最后呈现部分,但我肯定会遇到语法问题:

def search_ajax
@categories = HelpCategory.find(:all)

if params[:query].not_blank? && params[:query] != "Search for help about..."
  @query = params[:query]
  @terms = params[:query].split.map {|t| "%#{t.downcase}%" }

  options = {
      :allow_partial_match => true,
      :order => 'fulltext_match_score DESC',
  }
  @results = SearchableText.search_model(HelpItem, @query, options).uniq
  @results = {"Search Results" => @results} if !@results.blank?
  @complicated_query = HelpItem.is_complicated_query?(@query)
  @search_included = true
else
  @results = HelpItem.all.group_by {|item| item.category.name rescue "Misc"}
  @search_included = false
end

render :partial => "results"
respond_to do |format|
  format.js do
    render :partial => 'results'
  end
  format.html do
    render :partial => 'results'
  end
end

某些respond_to区域已被注释掉。我收到错误消息:

ActionController::DoubleRenderError in FaqController#search_ajax

我知道如果我将remote => true帮助器添加到我的表单中,控制器操作需要响应js。我很确定这是一个语法错误。 respond_to也在控制器操作结束时出现。

1 个答案:

答案 0 :(得分:1)

删除

render :partial => "results"

在respond_to块上方