小编Tri*_*nic的帖子

无法访问attr_accessor定义的变量

我正在使用Thinking Sphinx来运行搜索,我得到了合适的ActiveRecord模型.问题是,我想在每个模型上创建一个合适的链接路径和文本,然后通过AJAX以JSON的形式将信息发送到浏览器.我使用以下内容来构建这些链接属性:

在控制器中:

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

Run Code Online (Sandbox Code Playgroud) 在模型中: 生成的json对象没有列出任何search_*属性,更不用说它们的值了.我尝试在方法中使用@search_link以及search_link .build_ajax_response
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 …

activerecord ruby-on-rails attr-accessor

6
推荐指数
1
解决办法
2787
查看次数