0

我正在尝试使用acts_as_api。我尝试按照他们的教程进行操作,但我得到了名义上的错误,当我转到相应的网页时,屏幕上什么也没有出现。这是我得到的:

应用程序/控制器/qtls_controller.rb

def show_json
  show_id = URI.decode(params[:id])
  show_id = show_id.gsub(/\s*$/,'')
  @qtls = Qtl.find_by_sql("select * from qtls where qtl_name like '%#{show_id}%' or qtl_symbol in (select qtl_symbol from obs_traits where qtl_symbol like '%#{show_id}%' or trait_name like '%#{show_id}%');")
  respond_to do |format|
    format.json { render_for_api :lis_json, :json => @qtls, :root => :qtls }
  end
end

应用程序/模型/qtl.rb

 class Qtl < ActiveRecord::Base
   self.primary_key = "qtl_name"
   acts_as_api

   api_accessible :lis_json do |t|
      t.add :qtl_name
      t.add :qtl_symbol
      t.add :qtl_symbol_id
      t.add :qtl_pub_name
      t.add :favorable_allele_source
      t.add :treatment
      t.add :lod
      t.add :marker_r2
      t.add :total_r2
      t.add :additivity
      t.add :map_collection
      t.add :entry_name
      t.add :lg
      t.add :left_end
      t.add :right_end
      t.add :nearest_marker
      t.add :species
      #t.add :trait_name
      t.add :comment
   end
     def species
       'phavu'
     end
     #def trait_name
 #       '' << ObsTrait.find_by_sql("select trait_name from obs_traits where entry_name = '#{entry_name}' and qtl_symbol = '#{qtl_symbol}'")[0].trait_name
  #   end
 end

编辑:

当我在 Rails 控制台中执行此操作时

@qtls.as_api_response :lis_json, :root => :qtls

如果这可能是问题,我会得到这个(没有根)idk:

   => [{:lod=>#<BigDecimal:8063e1cb0,'0.0',9(18)>, :comment=>"Marker-Interval: L040.75–BMd027", :qtl_symbol=>"Pl ht", :entry_name=>"SRK20120130.4", :left_end=>#<BigDecimal:8063e1a08,'0.2964E2',18(18)>, :marker_r2=>#<BigDecimal:8063e1c38,'0.1952E0',9(18)>, :qtl_symbol_id=>"2-2", :right_end=>#<BigDecimal:8063e1990,'0.4503E2',18(18)>, :total_r2=>#<BigDecimal:8063e1bc0,'0.4812E0',9(18)>, :qtl_pub_name=>"Plh1-2", :nearest_marker=>"X010.85", :additivity=>#<BigDecimal:8063e1b48,'0.19E0',9(18)>, :favorable_allele_source=>"G2333", :species=>"phavu", :map_collection=>"G2333_x_G19839", :treatment=>"Darien high phosphorus", :qtl_name=>"Pl ht 2-2", :lg=>"Pv04"}, {:lod=>#<BigDecimal:8063e1800,'0.0',9(18)>, :comment=>"Marker-Interval: BM189–BMd036", :qtl_symbol=>"Pl ht", :entry_name=>"SRK20120130.4", :left_end=>#<BigDecimal:8063e1558,'0.877E1',18(18)>, :marker_r2=>#<BigDecimal:8063e1788,'0.1399E0',9(18)>, :qtl_symbol_id=>"2-1", :right_end=>#<BigDecimal:8063e14e0,'0.1526E2',18(18)>, :total_r2=>#<BigDecimal:8063e1710,'0.5119E0',9(18)>, :qtl_pub_name=>"Plh1-1", :nearest_marker=>"BM189", :additivity=>#<BigDecimal:8063e1698,'0.14E0',9(18)>, :favorable_allele_source=>"G2333", :species=>"phavu", :map_collection=>"G2333_x_G19839", :treatment=>"Popayan", :qtl_name=>"Pl ht 2-1", :lg=>"Pv03"}, {:lod=>#<BigDecimal:8063e1350,'0.0',9(18)>, :comment=>"Marker-Interval: PV-ctt001–BM161", :qtl_symbol=>"Pl ht", :entry_name=>"SRK20120130.4", :left_end=>#<BigDecimal:8063e10a8,'0.8743E2',18(18)>, :marker_r2=>#<BigDecimal:8063e12d8,'0.1403E0',9(18)>, :qtl_symbol_id=>"2-3", :right_end=>#<BigDecimal:8063e1030,'0.9362E2',18(18)>, :total_r2=>#<BigDecimal:8063e1260,'0.5276E0',9(18)>, :qtl_pub_name=>"Plh1-3", :nearest_marker=>"PV-ctt001", :additivity=>#<BigDecimal:8063e11e8,'0.14E0',9(18)>, :favorable_allele_source=>"G2333", :species=>"phavu", :map_collection=>"G2333_x_G19839", :treatment=>"Popayan", :qtl_name=>"Pl ht 2-3", :lg=>"Pv04"}]

这有效:

 render :json => @qtls.as_api_response( :lis_json, :root => 'qtls')

但它不会像在控制台中那样产生根...

4

1 回答 1

1

服务器似乎不接受您要求的格式。

确保您的请求确定了您想要的响应格式。在 Rails 中是默认设置,您可以将其添加到 url 例如posts/1.json或将其添加为 HTTP Accept-Header。

检查您的日志文件以查看操作是如何处理的。它应该这样说:

Started GET "/qtls/show_json.json" for 127.0.0.1 at 2012-07-23 19:53:11 Processing by QtlsController#show_json as JSON

但我想在你的情况下,它会说这样的话:

Processing by QtlsController#show_json as HTML

如果没有给出响应格式,Rails 默认为 HTML。但是您只允许在渲染块中使用 json 响应。

respond_to do |format|
  format.json { render_for_api :lis_json, :json => @qtls, :root => :qtls }
end

这就是 Rails 以 406 响应的原因。它假定客户端需要 HTML(默认格式)但该respond_to块只允许 json。

如果只是调用render :json它有效,因为您不区分响应格式,您只需强制控制器以 json 响应。

于 2012-07-23T18:10:22.270 回答