2014-03-19 46 views
0

在Inspections#show的上下文中,我使用will_paginate遍歷項目集合,當客戶點擊will_paginate的頁面對象列表中的另一個頁面的鏈接時,它的Score將被髮布到數據庫。因爲will_paginate不POST很好地工作,我用我已經在這裏摘錄使用this post答案:爲什麼我的JS腳本不是在Rails4中的form.submit()?

http://jsfiddle.net/sam452/pWrg3/4/

HTML

<div class="pagination"><a class="previous_page" rel="prev start" href="/inspections/1?page=1">&#8592; Previous</a> <a rel="prev start" href="/inspections/1?page=1">1</a> <em class="current">2</em> <a rel="next" href="/inspections/1?page=3">3</a> <a href="/inspections/1?page=4">4</a> <a href="/inspections/1?page=5">5</a> <a href="/inspections/1?page=6">6</a> <a class="next_page" rel="next" href="/inspections/1?page=3">Next &#8594;</a></div> 

<div class="item_enclosure row"> 
    <div class="item_text small-9 large-9 columns"> 
     Change Fund/Petty Cash (810) reviewed daily? 
    </div> 
    <div class="small-3 columns"> 
     <div class="item_scoring"> 
      <div class="row"> 
       <div class="item_score item-8 small-6 columns"> 
        <form accept-charset="UTF-8" action="/scores" class="new_score" data-remote="true" id="new_score" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /></div> 
        <input id="score_score_item" name="score[score_item]" placeholder="5" type="text" /> 
        <input id="score_item_id" name="score[item_id]" type="hidden" value="8" /> 
        <input id="score_inspection_id" name="score[inspection_id]" type="hidden" value="1" /> 
        <input name="commit" type="submit" value="Edit inspection" /> 

  </div> 
     </div> 
    </div> 
</div> 

app/assets/javascripts/inspection.js

$(".pagination a").click(function(e){e.preventDefault(); 
var tp_id = $(this).attr('href').split('?page=')[1]; 
$('form').append("<input type='hidden' name='page' value='"+ tp_id +"'>"); 
$('form').submit(); 
}); 

應用程序/視圖/ inspection.js.erb

$('body').append('<%= @score.score_item %>') 

應用程序/視圖/檢查/ show.html.erb

<%= will_paginate @items %> 
<% for item in @items %> 
    .. other html suppressed 
     <div class="item_score item-<%= item.id %> small-6 columns"> 
        <%= form_for @score, remote: true do |f| %> 
        <%= f.text_field :score_item, placeholder: item.high_score %> 
        <%= f.hidden_field :item_id, value: item.id %> 
        <%= f.hidden_field :inspection_id, value: params[:id] %> 
        <%= f.submit submit_text %> 
        <% end %> 
     </div> 

    .. other html suppressed 
<% end %> 

inspection.rb

class Inspection < ActiveRecord::Base 
    has_many :scores 
    accepts_nested_attributes_for :scores 
end 

得分。 rb

class Score < ActiveRecord::Base 
belongs_to :inspection 
end 

scores_controller.rb

class ScoresController < ApplicationController 
    def create 
    @score = Score.new(score_params) 
      @inspection = Inspection.find(@score.inspection_id) 
    if @score.save 
     binding.pry 
     respond_to do |format| 
     format.html {redirect_to inspection_path(@inspection, page: params[:page]), notice: "Scorex was accepted."} 
     format.js 
     end 

      else 
     render action: 'new' 
    end 
end 

    private 
    def score_params 
    params.require(:score).permit(:score_item, :inspection_id, :item_id, :page) 
    end 
end 

inspection_controller.rb

class InspectionsController < ApplicationController 
    def show 
    #binding.pry 
    @inspection = Inspection.find(params[:id]) 
    @items = Item.where(:id == @survey.id).order("sub_category").page(params[:page]).per_page(1) 
    @score = @inspection.scores.build 
    end 
end 

所以對於JS,每個作品的每一行。第一個收益正確。我通過交換Alert()的代碼來測試這一點,以確保preventDefault正常工作。第二個產生變量的正確值,如console.log()所示。第三個作品是因爲在控制檯中,我在瀏覽器中看到了代碼。提交作品和重定向發生,除了:頁面參數不傳遞。

單擊will_paginate鏈接將在右側執行GET:page參數。在服務器日誌中沒有POST操作。就好像preventDefault()沒有被識別一樣。爲此,當我查看Chrome的來源並點擊檢查時,我會看到結果:「1?page = 2」。也許Chrome不喜歡我的JS?我確定這是我忽略的一些愚蠢的東西。

我試圖刪除form_for的ajax調用,但沒有什麼不同。這篇文章的作品,但沒有找到頁面參數,因此它默認爲1.

爲什麼不是表單submit()發生,爲什麼不傳遞頁面參數?感謝名單,SAM

+0

http://jsfiddle.net/pWrg3/10/ 我的交: UTF8:✓ 得分[score_item]: 得分[ITEM_ID ]:8 得分[inspection_id]:1 頁面:4 –

+0

是的,請轉成回答,我會很樂意接受!謝謝。 – sam452

回答

1

My fiddle

在你的jsfiddle您有一個多餘的空格:d

< input 

有沒有jQuery的包括和$(函數(){在beggining將是不錯的;)

還增加

$('form#new_score').append(
$('form#new_score').submit(

我的帖子:UTF8:✓得分[score_item]:得分[ITEM_ID]:8得分[inspection_id] :1 頁:4

相關問題