I'm trying to use acts_as_api. I tried following their tutorial, but I got the titular error and nothing appeared on my screen when I went to the appropriate web page. Here's what I got:
app/controllers/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
app/models/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
EDIT:
when I do this in the rails console
@qtls.as_api_response :lis_json, :root => :qtls
I get this (without roots) idk if that could be the problem:
=> [{: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"}]
This works:
render :json => @qtls.as_api_response( :lis_json, :root => 'qtls')
but it doesn't produce the roots just like in the console...
It seems like the server does not accept the format you are asking for.
Make sure your request determines your desired response format. Be default in Rails, you can either add it to the url eg posts/1.json
or add it as a HTTP Accept-Header.
Check your log file to see how the action is processed. It will should say something like:
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
But I guess in your case, it will say something like this:
Processing by QtlsController#show_json as HTML
Rails defaults to HTML if no response format is given. But you only allow json responses in the rendering block.
respond_to do |format|
format.json { render_for_api :lis_json, :json => @qtls, :root => :qtls }
end
This is why Rails responds with 406. It assumes that the clients wants HTML (default format) but the respond_to
block only allows json.
If you just call render :json
it works, because you don't distinguish between response formats, you just force the controller to respond with json.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.