2015-01-08 75 views
0

我不知道這段代碼有什麼問題。我想從網址獲取數據,並在用戶將該網址粘貼到文本框中時顯示它們。這裏是我的代碼:使用Ajax和Nokogiri從rails中提取URL中的數據

應用程序/資產/ JavaScript的:

$(document).ready(function() { 

    $("#post_content").bind('paste', function(e) { 

     var el = $(this); 

     setTimeout(function() { 
      var text = $(el).val(); 

      // send url to service for parsing 
      $.ajax('/links/linkpreview', { 
       type: 'POST', 
       data: { url: text }, 
       success: function(data,textStatus,jqXHR) { 
        // handle received data 
        $("#preview-title").text(data['title']); 
       }, 
       error: function() { alert("error"); } 
      }); 
     }, 100); 
    }); 

}); 

應用程序/控制器/ links_controller.rb:

def linkpreview 
    url = params[:url] 
    doc = Nokogiri::HTML(open(url), nil, 'UTF-8') 

    title = "" 
    description = "" 
    url = "" 
    image_url = "" 

    doc.xpath("//head//meta").each do |meta| 
     if meta['property'] == 'og:title' 
      title = meta['content'] 
     elsif meta['property'] == 'og:description' || meta['name'] == 'description' 
      description = meta['content'] 
     elsif meta['property'] == 'og:url' 
      url = meta['content'] 
     elsif meta['property'] == 'og:image' 
      image_url = meta['content'] 
     end 
    end 

    if title == "" 
     title_node = doc.at_xpath("//head//title") 
     if title_node 
      title = title_node.text 
     elsif doc.title 
      title = doc.title 
     else 
      title = param_url 
     end 
    end 

    if description =="" 
     #maybe search for content from BODY 
     description = title 
    end 

    if url =="" 
     url = param_url 
    end 

    render :json => {:title => title, :description => description, :url => url, :image_url => image_url} and return 

end 

應用程序/視圖/鏈接/ _form.html.erb:

<div class="form-group"> 
    <%= f.label :url %><br> 
    <%= f.text_field :url, class: "form-control", id: "post_content" %> 
</div> 
<div id="preview-title"></div> 

回答

0
  1. 檢查發佈請求是否正確通過瀏覽器通過chrome發送開發工具。
  2. 檢查rails應用程序是否正確接收來自應用程序日誌的請求。
  3. 檢查rails應用程序是否正確發送響應。
  4. 檢查瀏覽器是否正確接收響應。
+0

我是新來的Rails,並沒有完全理解Ajax,你能告訴我問題在哪裏,爲什麼我的代碼不工作? –

+0

閱讀代碼我找不到任何問題。所以請檢查列表或粘貼JavaScript控制檯日誌和Rails應用程序日誌。如果你不知道如何,請詢問。 –

+0

只需正確寫入routes.rb即可。 –

0

這裏是在Javascript控制檯中的錯誤:

POST http://localhost:3000/links/linkpreview 404 (Not Found) 

這裏是日誌消息:

tarted POST "/links/linkpreview" for 127.0.0.1 at 2015-01-08 17:59:03 +0700 

的ActionController :: RoutingError(無路由匹配[POST]「/鏈接/ linkpreview「): web-console(2.0.0)lib/action_dispatch/debug_exceptions.rb:22:在middleware_call' web-console (2.0.0) lib/action_dispatch/debug_exceptions.rb:13:in調用' actionpack(4.2.0)lib/action_dispatch/middleware/show_exceptions.rb:30:in call' railties (4.2.0) lib/rails/rack/logger.rb:38:in call_app ' railties(4.2.0)LIB /導軌/齒條/ logger.rb:20:在block in call' activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in塊標記' 的ActiveSupport(4.2.0)LIB/active_support/tagged_logging.rb:26:在tagged' activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in標記」 (4.2.0)lib/rails/rack/logger.rb:20:在call' actionpack (4.2.0) lib/action_dispatch/middleware/request_id.rb:21:in調用' rack(1.6.0)lib/rack/methodoverride.rb:22:在call' rack (1.6.0) lib/rack/runtime.rb:18:in調用' activesupport(4.2.0) lib/active_support/cache/strategy/local_cache_middleware.rb:28:call' rack (1.6.0) lib/rack/lock.rb:17:in調用' actionpack(4.2.0)lib/action_dispatch/middleware/static.rb:113:在call' rack (1.6.0) lib/rack/sendfile.rb:113:in調用' railties(4.2.0)lib/rails/engine.rb:518:在call' railties (4.2.0) lib/rails/application.rb:164:in中調用' rack(1.6.0)lib/rack/lock.rb:17:in call' rack (1.6.0) lib/rack/content_length.rb:15:in電話' 機架(1.6.0)lib/rack/handler/webrick.rb:89:在service' /home/tuong/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/webrick/httpserver.rb:138:in服務' /home/tuong/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0 .0/webrick/httpserver.rb:94:in run' /home/tuong/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/webrick/server.rb:295:in block in start_thread'

Rendered /home/tuong/.rvm/gems/ruby-2.0.0-p481/gems/web-console-2.0.0/ lib/action_dispatch/templates/rescues/_trace.text.erb(0.3ms) Rendered /home/tuong/.rvm/gems/ruby-2.0.0-p481/gems/web-console-2.0.0/lib/action_dispatch /templates/rescues/routing_error.text.erb(3.5ms)