2015-04-13 137 views
0

嘿傢伙我需要知道一些問題的答案。 第一個當我不從控制器返回任何東西時,我該如何處理ajax調用,因爲它總是要求我調用被調用的函數的視圖,但是因爲我已經使用ajax調用了該函數,它怎麼能有一個視圖。我用它在ROR(Ruby on Rails的))ajax控制在軌道上

bookmark.html.erb

<div class="row"> 
     <div id="page-wrapper-lbms" class="small-12 medium-12 large-12 columns dashboard-page"> 

     <input type="hidden" id="bookmarked_against_req_id" value=<%= @data2%> /> 
     <% 
     if @requests != nil 
     @requests.each.with_index do |request,index| 

      if request != nil 
      %> 
     <header> 
     <div style="text-align:center"> 
      <a href="#" onclick="ajax_bookmark('<%= request.id %>' , '<%[email protected]_id%>')" ><span class="fa fa-bookmark"></span></a> 
     </div> 
     <h2 style="text-align:center"><%= request.title %></h2> 

     <div style="text-align:center"> 
      <h6 style="color:#aaaaaa"><strong><%= request.is_service%></strong></h6> 
     </div> 
     </header> 
     <div class ="row"> 
     <div class="small-12 medium-12 large-8 columns"> 

     <div id="request-content-lbms" class="panel"> 
      <p> 
      <%= request.description%> 
      </p> 
     </div> 
     <div id="list-request-cat-lbms"> 
      <span class="fa fa-tag"></span> 
      <% if @tag[index] != nil 
       @tag[index].each do |cat| %> 
       <%= cat.title %> 
      <%end%> 
      <%end%> 
     </div> 
     </div> 
     <aside class="small-12 medium-12 large-4 columns" style="padding-left:10%"> 

      <h3 ><strong>Syed Muhammad Ali Gardezi</strong></h3> 
      <time><%= request.created_at %></time><br /> 
      <%end%> 
      <button class="button tiny">Schedule Meeting</button> 
     </aside> 
    </div> 
    </div> 

    <%end%> 
    <%end%> 




    <script> 
    function ajax_bookmark(abc , cde){ 

     var cde = $('#bookmarked_against_req_id').val(); 
     $.ajax({ 
    type: "GET", 
    url: 'bookmark_request', 
    data: { 
     d:abc, 
     d1:cde 
    }, 
     error:function(){ 

    }, 
    dataType: 'json', 
    //beforeSend:function(){}, 
    //complete:function(o,s){}, 
    success:function(data){ 
     location.href ="/requests/bookmark" 
    }, 
    type: 'get' 
    }); 
    } 
    </script 

bookmark_request

def bookmark_request() 

    data = params[:d] 
    data1 = params[:d1] 
    data2 = data1#["$oid"]#.split('"' , 6) 
    #asd 
    request_bookmarked = Request.getRequest(data) 
    bookmarked_against_Request = Request.getRequest(data2) 
    request_bookmarked_2 = request_bookmarked 
    bookmarked_against_Request_2 = bookmarked_against_Request 
    #asd 
    if bookmarked_against_Request_2[:favourites] 
     bookmarked_against_Request_2[:favourites] << request_bookmarked[:id] 
    else 

     bookmarked_against_Request_2[:favourites] = Array.new 
     bookmarked_against_Request[:favourites] = Array.new 
     bookmarked_against_Request_2[:favourites] << request_bookmarked[:id] 
    end 

    if request_bookmarked_2[:favourites_of] 
     request_bookmarked_2[:favourites_of] << bookmarked_against_Request[:id] 

    else 
     request_bookmarked_2[:favourites_of] = Array.new 
     request_bookmarked[:favourites_of] = Array.new 
     request_bookmarked_2[:favourites_of] << bookmarked_against_Request[:id] 
    end 

    request_bookmarked.update(Hash['favourites' ,request_bookmarked_2[:favourites]]) 
    bookmarked_against_Request.update(Hash['favourites' ,bookmarked_against_Request_2[:favourites]]) 
    #asd 

    bookmarks = Bookmark.where(request_id: bookmarked_against_Request[:id]).first() 
    #asd 
    b = bookmarks 
    if bookmarks != nil 
    #bookmarks.each { |bookmark| bookmark.update_attributes(corsponding_requests: request_bookmarked[:id]) } 
    b[:corsponding_requests] << request_bookmarked[:id] 
    b.update(Hash['corsponding_requests' , b[:corsponding_requests]])  

    else 

    bookmark = Hash.new 
    bookmark["owner_req"] = session[:user] 
    bookmark["request_id"] = bookmarked_against_Request[:id] 
    bookmark["corsponding_requests"] = Array.new 
    bookmark["corsponding_requests"] << request_bookmarked[:_id] 
    Bookmark.createBookmark(bookmark) 

    end 

    flash[:notice] = "Request has been bookmarked successfully." 
end 

現在,當過我運行這個功能,我需要它去到成功ajax的功能bookmark.html.erb但問題是它說我找不到視圖。什麼是我在這裏做錯了

第二當我需要返回的東西我已閱讀,我需要做一個HTML助手,但我不知道什麼是一個HTML助手是。我怎麼能返回一個字符串返回到AJAX功能

任何幫助將不勝感激(我是新來的AJAX)

回答

0

要去嘗試回答你:

第一: 如果你有一個您Ajax調用專項行動,你只需要:

def custom_action 
end 

,這會使得custom_action.js.erb如果調用與阿賈克斯,例如形式remote: true

如果你打電話給CRUD-操作之一,需要能夠以多種格式迴應:

def show 
    @book = Book.find(params[:id]) 
    respond_to do |format| 
    format.html # will render show.html.erb 
    format.js # will render show.js.erb 
    end 
end 

如果你把你的觀點鏈接:

link_to('Remote Ajax Call', book_path(@book), remote: true) 

此鏈接將調用方法顯示並渲染爲:js。 它會尋找app/views/books/show.js.erb這可能看起來像:

<% if @book.present? %> 
    $('#book-title').html("<%[email protected]%>"); 
<%end%> 

而且在你看來,接近上面的鏈接,這將進而改變一個html標籤裏面的文字與我倒是book-title

<div id="book-title"> 
</div> 

二:

我不明白你的

的意思......需要返回的東西......

你可以添加一個例子?

希望這可以幫助你一點。

編輯: 這是我試着去了解你想要做什麼: 在您看來:

<%= hidden_field_tag :bookmarked_against_req_id, value: @data2 %> 
    <% if @requests != nil %> 
    <% @requests.each.with_index do |request,index| %> 
     <% if request.present? %> 
     <%= link_to 'Bookmark it', bookmark_request_path(request), remote: true %> 
     <%= request.title %> 
     <%= request.is_service%> 
     <%= request.description%> 
     <% if @tag[index].present? %> 
      <% @tag[index].each do |cat| %> 
      <%= cat.title %> 
      <%end%> 
     <%end%> 
     <%= request.created_at %> 
     <%end%> 
    <%end%> 
<%end%> 

在你的控制器:

def bookmark_request() 
    request_bookmarked = Request.getRequest(params[:id]) # the params[:id] should be set in the request. 
    bookmarked_agains_Request = Request.getRequest(params[:bookmarked_against_req_id) # This is defined in your hidden_field_tag 

當你的行動bookmark_request完成。如果請求是AJAX,你將呈現的JavaScript文件bookmark_request.js.erb

在這裏,你應該有你把你的視圖腳本:

如果資源被保存,你應該把JavaScript呈現「成功」,或者它失敗了,你應該這樣做。

flash[:notice] = "Request has been bookmarked successfully."將不會被您的AJAX請求所理解。

+0

將這個format.html帶我到ajax的成功功能。因爲我需要在那裏做點什麼 並在第二個像我需要返回一個字符串或一個對象,如 「偉大」或對象說的書籍 – Gardezi

+0

我添加了一個關於如何使用ajax響應的例子。如果我理解正確,我認爲這也是你在第二部分想要的。 – davidwessman

+0

事情是我需要返回值首先返回在ajax成功函數 第二件事是當我沒有返回任何東西開始給我的錯誤,應用程序找不到視圖。我只需要在這種情況下返回到ajax成功函數 – Gardezi

0

您可能會想看看導軌指南 - 它們是一般接地的重要來源:http://guides.rubyonrails.org。根據請求格式(ajax與html)返回不同的響應,請參閱http://guides.rubyonrails.org/action_controller_overview.html#rendering-xml-and-json-data和@ davidwessman的回答。

通常,您通常會從ajax請求返回JSON數據;因此,例如,在控制器你可能會說

respond_to do |format| 
    format.js {render json: @my_ojects.to_json} 
end 

注意,有幾種方式來呈現JSON,包括像rabl寶石。如果你像這樣返回json,你不需要渲染rails視圖(除非你使用視圖來構建你的json,就像rabl做的或者在davidwessman的js.erb的回答中暗示的那樣)。

希望有所幫助。

+0

Matt還有一個問題,我怎樣才能獲得ajax的成功功能,因爲我需要在format.html中做一些事情,並將我帶到與我的功能相對應的視圖。我有一個函數讓我們說bookmark_request。我只是更新了一些東西,然後它需要返回到bookmark.html.erb – Gardezi

+0

,我需要從一個函數渲染一個JSON讓我們說一個視圖讓我們說a.html.erb我該怎麼做,因爲簡單渲染json:---不會把我帶回到ajax的成功函數 – Gardezi