我正在使用Thinking Sphinx来运行搜索,并且可以正确获取ActiveRecord模型。问题是,我想要在每个模型上创建适当的链接路径和文本,然后通过AJAX以JSON的形式将信息发送到浏览器。我正在使用以下代码来构建这些链接属性:
在控制器中:
生成的JSON对象没有列出search_*属性,更别说为它们设置值了。我已经尝试使用@search_link以及build_ajax_response方法中的search_link。 我做错了吗?还有其他可能会干扰的因素吗?
在控制器中:
class FindController < ApplicationController
def tag_results
@results = ThinkingSphinx.search(params[:terms])
@results.each do |result|
result.build_ajax_response
end
respond_to do |format|
format.html
format.json { render :json => @results }
end
end
end
在模型中:
class TaggedItem < ActiveRecord::Base
attr_accessible :name
attr_accessor :search_link, :search_text
def build_ajax_response
self.search_link = Rails.application.routes.url_helpers.tagged_item_path(self.id)
self.search_text = self.name
end
end
生成的JSON对象没有列出search_*属性,更别说为它们设置值了。我已经尝试使用@search_link以及build_ajax_response方法中的search_link。 我做错了吗?还有其他可能会干扰的因素吗?
build_ajax_response
吗?有什么异常抛出吗?有什么设置吗? - Draiken