2017-04-24 36 views
0

我PARAMS哈希看起來是這樣的:Parameters: {"id"=>"6", "bonus_id"=>"2", "bonus_title"=>"deposit-match-row"},這是我的控制器:無法找到與「ID」獎金=,而在軌道安慰一切正常

def load_more_bonuses 
    @more_casinos = [] 
    @bonus_title = params[:bonus_title] 
    bonus = Bonus.find(params[:bonus_id]) 
    @more_casinos << bonus.casinos.where('casinos.id >?', params[:id]).order(id: :asc).limit(6).uniq 

    respond_to do |format| 
     format.js 
    end 
    end 

我得到以下錯誤:ActiveRecord::RecordNotFound (Couldn't find Bonus with 'id'=)在軌道控制檯中一切正常,做:bonus = Bonus.find(2)給我我的獎金記錄。上面的代碼有什麼問題,可以做些什麼來擺脫錯誤?謝謝。

編輯

全服務器日誌:

Started GET "/load_more_bonuses?id=6&bonus_id=2&bonus_title=deposit-match-row" for 127.0.0.1 at 2017-04-24 12:08:00 +0300 
Processing by BonusesController#load_more_bonuses as JS 
    Parameters: {"id"=>"6", "bonus_id"=>"2", "bonus_title"=>"deposit-match-row"} 
Started GET "/load_more_bonuses" for 127.0.0.1 at 2017-04-24 12:08:00 +0300 
Processing by BonusesController#load_more_bonuses as JS 
    Casino Load (7.2ms) SELECT "casinos".* FROM "casinos" WHERE "casinos"."casino_of_the_month" = $1 LIMIT $2 [["casino_of_the_month", true], ["LIMIT", 1]] 
    Casino Load (1.4ms) SELECT "casinos".* FROM "casinos" WHERE "casinos"."casino_of_the_month" = $1 LIMIT $2 [["casino_of_the_month", true], ["LIMIT", 1]] 
    Bonus Load (1.2ms) SELECT "bonuses".* FROM "bonuses" WHERE "bonuses"."id" = $1 LIMIT $2 [["id", 2], ["LIMIT", 1]] 
    Bonus Load (15.7ms) SELECT "bonuses".* FROM "bonuses" WHERE "bonuses"."id" = $1 LIMIT $2 [["id", nil], ["LIMIT", 1]] 
    Rendering bonuses/load_more_bonuses.js.erb 
Completed 404 Not Found in 27ms (ActiveRecord: 17.1ms) 



ActiveRecord::RecordNotFound (Couldn't find Bonus with 'id'=): 

    Casino Load (2.2ms) SELECT DISTINCT "casinos".* FROM "casinos" INNER JOIN "casino_bonuses" ON "casinos"."id" = "casino_bonuses"."casino_id" WHERE "casino_bonuses"."bonus_id" = $1 AND (casinos.id >'6') ORDER BY "casinos"."id" ASC LIMIT $2 [["bonus_id", 2], ["LIMIT", 6]] 
app/controllers/bonuses_controller.rb:75:in `load_more_bonuses' 
    Rendering /home/spacechick/.rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb 
    Rendering /home/spacechick/.rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.text.erb 
    Rendered /home/spacechick/.rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.text.erb (0.7ms) 
    Rendering /home/spacechick/.rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb 
    Rendered /home/spacechick/.rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (0.8ms) 
    Rendering /home/spacechick/.rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb 
    Rendered /home/spacechick/.rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb (1.2ms) 
    Rendered /home/spacechick/.rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb (26.8ms) 
    (0.5ms) SELECT COUNT(*) FROM "ratings" WHERE "ratings"."casino_id" = $1 [["casino_id", 10]] 
    (0.4ms) SELECT SUM("ratings"."score") FROM "ratings" WHERE "ratings"."casino_id" = $1 [["casino_id", 10]] 
    CACHE (0.0ms) SELECT COUNT(*) FROM "ratings" WHERE "ratings"."casino_id" = $1 [["casino_id", 10]] 
    CACHE (0.0ms) SELECT COUNT(*) FROM "ratings" WHERE "ratings"."casino_id" = $1 [["casino_id", 10]] 
    CACHE (0.0ms) SELECT SUM("ratings"."score") FROM "ratings" WHERE "ratings"."casino_id" = $1 [["casino_id", 10]] 
    CACHE (0.0ms) SELECT COUNT(*) FROM "ratings" WHERE "ratings"."casino_id" = $1 [["casino_id", 10]] 
    Rendered bonuses/_load_more_casinos_list.html.erb (150.9ms) 
    Rendered bonuses/load_more_bonuses.js.erb (154.1ms) 
Completed 200 OK in 193ms (Views: 159.7ms | ActiveRecord: 11.5ms) 

EDIT 2

_load_more_casinos_list.html.erb

<% @more_casinos.flatten.uniq.sort_by{ |id| id[:id] }.reverse!.take(6).each do |casino| %> 
    <div class="col-lg-4 col-md-6 col-sm-12 col-xs-12"> 
    <div class="bonus-casino-index-item" data-id="<%= casino.id %>"> 
     <div class="bonus-casinos-index-item-flip"> 
     <div class="front"> 
      <div class="bonus-casino-background" style="background-color: <%= casino.background_color %>"> 
      <div class="bonus-info-sign"> 
       <span class="glyphicon glyphicon-info-sign"></span> 
      </div> 
      <% if casino.new === true %> 
       <div class="bonus-casino-new"></div> 
      <% elsif casino.hot === true %> 
       <div class="bonus-casino-hot"></div> 
      <% elsif casino.exclusive === true %> 
       <div class="bonus-casino-exclusive"></div> 
      <% end %> 
      <div class="bonus-casino-logo">Logo will be here</div> 
      <div class="bonus-casino-rating"> 
       <div class="rateit" data-rateit-value="<%= casino.average_rating %>" data-rateit-ispreset="true" data-rateit-readonly="true" data-rateit-mode="font" style="font-size:20px"></div> 
      </div> 
      <div class="bonus-casino-bonus-info"> 
       <%= casino.bonus_info %> 
      </div> 
      </div> 
     </div> 
     <div class="back"> 
      <div class="bonus-casino-background-back"> 
      <div class="bonus-casino-back-close"> 
       <span class="glyphicon glyphicon-remove-sign"></span> 
      </div> 
      <p class="bonus-casino-bonus-info-back"><%= casino.bonus_info.truncate(50) %></p> 
      <p class="bonus-casino-average-rating">Score: <%= casino.average_rating.round(2) %></p> 
      <div class="bonus-casino-bonus-description"><%= strip_tags(casino.description).truncate(120) %></div> 
      </div> 
     </div> 
     </div> 
     <div class="bonuses-page-casino-list-links"> 
     <%= link_to 'Review', casino, class: 'bonus-casino-index-item-review' %> 
     <%= link_to 'Join now', '#', class: 'bonus-casino-index-item-join-now' %> 
     </div> 
    </div> 
    </div> 
<% end %> 

編輯3

puts @more_casinos給我:

Casino Load (4.1ms) SELECT "casinos".* FROM "casinos" WHERE "casinos"."casino_of_the_month" = $1 LIMIT $2 [["casino_of_the_month", true], ["LIMIT", 1]] 
    Casino Load (1.8ms) SELECT DISTINCT "casinos".* FROM "casinos" INNER JOIN "casino_bonuses" ON "casinos"."id" = "casino_bonuses"."casino_id" WHERE "casino_bonuses"."bonus_id" = $1 AND (casinos.id >'6') ORDER BY "casinos"."id" ASC LIMIT $2 [["bonus_id", 2], ["LIMIT", 6]] 
    Bonus Load (0.4ms) SELECT "bonuses".* FROM "bonuses" WHERE "bonuses"."id" = $1 LIMIT $2 [["id", nil], ["LIMIT", 1]] 
#<Casino:0x000000082c8820> 

而且load_more_bonuses.js.erb

<% if @more_casinos.any? %> 
    $('#<%= @bonus_title.parameterize %>-row').append('<%= j render 'bonuses/load_more_casinos_list' %>'); 
<% end %> 

EDIT 4

Ajax調用在application.js

$('.bonus-casinos-load-more').click(function() { 
    var lastId = $(this).parent().parent().parent().children('.first-row').children().last().children().attr('data-id'); 
    var bonusId = $(this).attr('data-id'); 
    var bonusTitle = $(this).parent().parent().parent().children('.first-row').attr('id'); 

    $.ajax({ 
     type: 'GET', 
     url: $(this).attr('href'), 
     data: { 
     id: lastId, 
     bonus_id: bonusId, 
     bonus_title: bonusTitle 
     }, 
     dataType: 'script' 
    }); 
    }); 
+0

你有一些'before_action'或'after_action'或什麼在你的控制器? – Iceman

+0

不,沒有'before_action'或'after_action' –

+0

'friendly_id'或這樣的寶石? – Iceman

回答

0

該問題應與視圖相關,因爲在您的日誌中,它會發送2個請求。

一個合法查詢字符串請求,一個請求查詢字符串爲空。

Started GET "/load_more_bonuses?id=6&bonus_id=2&bonus_title=deposit-match-row" for 127.0.0.1 at 2017-04-24 12:08:00 +0300 
Processing by BonusesController#load_more_bonuses as JS 
Parameters: {"id"=>"6", "bonus_id"=>"2", "bonus_title"=>"deposit-match-row"} 

Started GET "/load_more_bonuses" for 127.0.0.1 at 2017-04-24 12:08:00 +0300 

在您的視圖文件中,它會發送另一個請求,但沒有參數。

它由ajax調用發送,然後檢查參數,找出它爲什麼是空的。在js代碼處添加一些console.log

+0

'puts params [:bonus_id]'給我一個'2'的值,所以它不是空的 –

+0

可以粘貼整個異常日誌嗎?我認爲這個例外地點可能不在你粘貼的方法。 –

+0

如果'params [:bonus_id]'確實是2,那麼錯誤必須在其他行上。 – Iceman