如何在开发和生产中获得相同的JSON响应?

时间:2016-03-28 21:25:59

标签: jquery-ui ruby-on-rails-4

我在搜索表单上使用jQuery-ui的自动填充功能。在开发中,请求命中我的索引页面,该页面返回JSON响应:

响应如此:

  

[{" ID":0," listing_id":0," CATEGORY_ID":0,"标题":&#34 ;天然梭织亚麻   戒指吊带","描述":"这款天然梭织亚麻吊带非常完美   让您和宝宝在任何气候下都感到舒适。随着光   舒适,它将符合你和你孩子的身体和   随着时间的推移变得越来越软。\ nSling是保持宝宝的好方法   关闭,双手自由。\ n穿着你的宝宝增加粘合力   鼓励皮肤与父母和皮肤接触和亲密   护理人员。婴儿吊索模仿子宫环境,让宝宝感觉到   安全又可靠。靠近时宝宝的需求很容易满足,   这意味着减少哭泣。婴儿吊索非常适合谨慎   无论你身在何处,母乳喂养。 \ n这款吊带适合   7-35磅之间的婴儿。\ n佩戴时一定要小心   你的宝宝。\ n 保持婴儿的脸部可见   在打宝宝之前练习穿着吊带   内部。\ n 在宝宝身上避免任何不安全的活动,例如   as:\ n吸烟,喝热饮,跑步,锻炼,烹饪,或   喝   酒精""价格":" 65.00"" CURRENCY_CODE":" CAD""量&# 34;:1,"标记":["吊索   戒指","吊环","幼儿","新生儿","天然亚麻","编织   包装","婴儿背带","编织吊带"," babywearing","婴儿包裹","婴儿   sling"]," category_path":[" Bags and   Purses"]," taxonomy_path":[" Accessories"," Baby Accessories"," Baby   Carriers"],"材料":["铝环","欧洲   亚麻""棉   螺纹&#34]," featured_rank":空," URL":" https://www.etsy.com/listing/272579256/natural-woven-linen-ring-sling?utm_source=etsyinventorymerger \ u0026utm_medium = API \ u0026utm_campaign = API"&#34 ;视图":19," num_favorers":0," shipping_template_id":6281647," shipping_profile_id":空,"图像&#34 ;: [" https://img1.etsystatic.com/135/0/6276910/il_170x135.987731269_rwab.jpg"" https://img1.etsystatic.com/139/0/6276910/il_170x135.987731277_1q29.jpg"" https://img1.etsystatic.com/140/0/6276910/il_170x135.987731279_q5lv.jpg&#34]," created_at&# 34;:" 2016-03-28T20:01:41.722Z""的updated_at":" 2016-03-28T20:04:52.721Z"},{ " ID":18," listing_id":269532744" CATEGORY_ID":269532744"标题":"编织   棉花异想天开的波浪吊环","描述":"这个吊带是   质地轻盈,坚固,采用100%纯棉制成。肩膀是   缝制舒适地保持你的手臂和宝宝的体重   平均分配。适合任何气候,适合任何装备。   \ n \ nSling是保持宝宝亲近和双手的好方法   通过鼓励皮肤到皮肤,给宝宝穿上衣服,增加粘合力   与父母和照顾者联系和亲密。婴儿吊索模仿   子宫环境,让宝宝感到安全。   宝宝的需求在靠近时很容易满足,这意味着更少   哭了。无论是婴儿吊索都非常适合谨慎的母乳喂养   你在哪里。 \ n这款吊带适合7-35岁之间的婴儿使用   磅。\ n佩戴宝宝时一定要小心。\ n 继续   宝宝的脸在任何时候都可见。\ n 练习穿着你的脸   在将婴儿放入室内之前吊索。\ n 避免任何不安全的活动   穿着你的宝宝,如:\ n吸烟,喝热饮,跑步,   锻炼,烹饪或饮酒   酒精""价格":" 65.00"" CURRENCY_CODE":" CAD""量&# 34;:1,"标记":["新   妈妈的礼物","婴儿沐浴礼物","新生儿吊带","婴儿包裹","戒指吊带   尾巴","编织棉质吊带""编织吊带","婴儿背带","戒指   sling","加拿大制造"]," category_path":["包袋和   Purses"]," taxonomy_path":[" Accessories"," Baby Accessories"," Baby   Carriers"],"材料":"棉","铝环","棉   螺纹&#34]," featured_rank":1," URL":" https://www.etsy.com/listing/269532744/woven-cotton-whimsical-waves-ring-sling?utm_source=etsyinventorymerger \ u0026utm_medium = API \ u0026utm_campaign = API"&#34 ;视图":42," num_favorers":3," shipping_template_id":6281647," shipping_profile_id":空,"图像&#34 ;: [" https://img1.etsystatic.com/115/0/6276910/il_170x135.927557949_lp3o.jpg"" https://img1.etsystatic.com/113/0/6276910/il_170x135.927557945_8km2.jpg"" https://img1.etsystatic.com/117/0/6276910/il_170x135.927557953_nyef.jpg"" {{3 }}"" https://img0.etsystatic.com/112/0/6276910/il_170x135.927814742_9wo0.jpg&#34]," created_at":" 2016-03-28T20:01:45.104Z",& #34;的updated_at":" 2016-03-28T20:04:56.129Z"}]

这是我的控制者:

def index
  respond_to do |format|
    format.html
    format.json do
      @etsy_products = EtsyProduct.search(params[:term])
      render json: @etsy_products, status: :ok, message: 'Success'
    end
  end

我的脚本收到返回响应并相应地对其进行格式化:

$ ->
  $('#etsy_products_search').autocomplete(
    minLength: 0
    source: '/'
    focus: (event, ui) ->
      $('#etsy_products_search').val ui.item.title
      false
    select: (event, ui) ->
      $('#etsy_products_search').val ui.item.title
      $('#etsy_products_search-description').html ui.item.description
      false
  ).autocomplete('instance')._renderItem = (ul, item) ->
    $('<li>')
      .attr({'title': item.description, 'data-toggle': 'tooltip', 'data-thumbnail': item.images[0], 'data-etsy-url': item.url})
      .append(item.title).appendTo ul

我的开发日志显示了这一点:

Started GET "/?term=woven" for ::1 at 2016-03-28 17:16:49 -0400
Processing by HomeController#index as JSON
  Parameters: {"term"=>"woven"}
  EtsyProduct Load (0.8ms)  SELECT "etsy_products".* FROM "etsy_products" WHERE (title ILIKE '%woven%')
Completed 200 OK in 18ms (Views: 13.6ms | ActiveRecord: 2.5ms)

我是按app部署的,然后在生产中对其进行了测试。 JSON响应为空,我的生产日志如下所示:

I, [2016-03-28T17:19:13.552941 #25285]  INFO -- : Started GET "/?term=woven" for (ip) at 2016-03-28 17:19:13 -0400
I, [2016-03-28T17:19:13.558963 #25285]  INFO -- : Processing by HomeController#index as JSON
I, [2016-03-28T17:19:13.559220 #25285]  INFO -- :   Parameters: {"term"=>"woven"}
D, [2016-03-28T17:19:13.565312 #25285] DEBUG -- :   EtsyProduct Load (1.0ms)  SELECT "etsy_products".* FROM "etsy_products" WHERE (title ILIKE '%woven%')
I, [2016-03-28T17:19:13.566088 #25285]  INFO -- : Completed 200 OK in 7ms (Views: 2.0ms | ActiveRecord: 1.0ms)

如果我进入生产服务器的控制台,并运行我的搜索脚本,它会返回所有条目。麻烦似乎是它将响应传递回请求。它回来了。我确定这是一件让我被忽视的蠢事,但我似乎无法在谷歌上找到正确的答案,或者,(我更有可能)我是提出错误的问题。

1 个答案:

答案 0 :(得分:0)

原来我完全走错了路。问题不在于响应,而在于数据库。你看,当我检查确保数据库有记录时,我正在做标准&#34; rails c&#34;生产服务器上的方法。我使用rake任务从API收集记录并将其写入数据库。总而言之,rake任务实际上是写入我服务器上的开发数据库,​​而不是生产。当我跑#34; rails c&#34;它让我访问该数据库而不是生产数据库。 Urgh!还有很多东西需要学习。希望这将有助于未来的另一个新手,以防他们像我一样被围墙。