Within the context of an Inspections#show, I am using will_paginate to iterate through a collection of items and its Score will be POSTed to the db when the customer clicks on a link for another page in will_paginate's list of page objects. Because will_paginate does not work well with POST, I've used the answer in this post which I've used in a snippet here:
http://jsfiddle.net/sam452/pWrg3/4/
html
<div class="pagination"><a class="previous_page" rel="prev start" href="/inspections/1?page=1">← 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 →</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="✓" /></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();
});
app/views/inspection.js.erb
$('body').append('<%= @score.score_item %>')
app/views/inspections/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
score.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
So for each line of the JS, each works. The first yields correctly. I tested this by swapping the code for an Alert() to ensure that preventDefault is working. The second yields the correct value for the variable, as noted by console.log(). The third works because in console I see the code in the browser. The submit works and redirect happens except the :page params are not passed.
Clicking the will_paginate link performs a GET to the right :page parameter. In the server logs there is NO POST action. As if the preventDefault() isn't being recognized. To that end, when I look in Chrome's Sources and click on Inspection I see the result: "1?page=2". Perhaps Chrome doesn't like my JS? I'm sure it's some silly thing I overlooked.
I've attempted to remove the ajax call out of the form_for but no different. The post works, but the page param isn't found so it defaults to 1.
Why isn't the form submit() happenening and why aren't the page params being passed? thanx, sam