Hello all,
I posted this on the railsforum website as well, and haven't received any response, so I figured I'd try and get a bit more exposure by seeing if anyone here and on the mailing list would be able to assist: I'm having an extremely strange issue with respect to partials.
I have an empty fieldset, with an id of "claim" that in the initial view, I have set to:
1. <fieldset id="claim"""> 2. <%= render :partial => 'claim', :layout => false %> 3. </fieldset>
In the render file, I have a legend followed by 5 divs setting up a 4-field form (1 select, 3 text inputs) and a button, which is conditionally either an "Add" or an "Edit", that I'm using Prototype to utilize AJAX submissions of the record being added or edited into the database.
Here's the top of the partial:
1. <legend>Claims</legend> 2. 3. <!-- Claims form --> 4. <% remote_form_for @full_quote_ho_claim, :url => 'Javascript.void(0)', :html => {:id => 'ClaimForm'} do |form| %> 5. <div> 6. <%= form.label :description, 'Description:' %> 7. <%= form.text_field :description, :size => 50, :maxlength => 50 %> 8. </div> 9. <div> 10. <%= form.label :cause_of_loss, 'Cause of Loss:' %> 11. <%= form.select :cause_of_loss, @causesOfLoss %> 12. </div> 13. <div> 14. <%= form.label :date_of_loss, 'Date of Loss:' %> 15. <%= form.text_field :date_of_loss, :size => 10, :maxlength => 10 %> 16. </div> 17. <div> 18. <%= form.label :amount_of_loss, 'Amount of Loss:' %> 19. <%= form.text_field :amount_of_loss, :size => 15, :maxlength => 15 %> 20. </div> 21. <div> 22. <% if @modify == false %> 23. <%= submit_to_remote 'add_claim_btn', 'Add Claim', :url => {:controller => :full_quote_ho_locs, :action => :add_claim, :id => @full_quote_ho_loc.id, :full_quote_ho_model_id => @full_quote_ho_model.id}, :update => {:success => 'claim', :failure => 'notice'} %> 24. <% else %> 25. <%= submit_to_remote 'update_claim_btn', 'Update Claim', :url => {:controller => :full_quote_ho_locs, :action => :update_claim, :id => @full_quote_ho_loc.id, :full_quote_ho_model_id => @full_quote_ho_model.id, :full_quote_ho_claim_id => @full_quote_ho_claim.id}, :update => {:success => 'claim', :failure => 'notice'} %> 26. <% end %> 27. </div> 28. <% end %>
This portion of the code always renders fine. Now, I'm trying to go back and display a listing directly underneath the form allowing a user to see all claims they have just added, edited, or removed. This portion shows up perfectly the first time, but doesn't show up after I add a claim, edit a claim, or remove a claim. It doesn't make sense to me, since the whole thing (top with the form, and bottom with the view) are all in the same partial, _claims.html.erb.
Here's the bottom of the partial:
1. <!-- Claims Listing, if any --> 2. <div style="clear: left"> 3. <% if !@full_quote_ho_claims.blank? %> 4. <table> 5. <tr> 6. <th>Description:</th> 7. <th>Cause of Loss:</th> 8. <th>Date of Loss:</th> 9. <th>Amount of Loss:</th> 10. <th>Modify</th> 11. <th>Remove</th> 12. </tr> 13. <% @full_quote_ho_claims.each do |claim| %> 14. <% remote_form_for claim, :url => 'Javascript.void(0)' do |form| %> 15. <tr> 16. <td><%= claim.description %></td> 17. <td><%= claim.cause_of_loss %></td> 18. <td><%= claim.date_of_loss %></td> 19. <td><%= claim.amount_of_loss %></td> 20. <td><%= submit_to_remote 'edit_claim_btn', 'Edit Claim', :url => {:controller => :full_quote_ho_locs, :action => :edit_claim, :id => @full_quote_ho_loc.id, :full_quote_ho_model_id => @full_quote_ho_model.id, :full_quote_ho_claim_id => claim.id}, :update => {:success => 'claim', :failure => 'notice'} %></td> 21. <td><%= submit_to_remote 'remove_claim_btn', 'Remove Claim', :url => {:controller => :full_quote_ho_locs, :action => :remove_claim, :id => @full_quote_ho_loc.id, :full_quote_ho_model_id => @full_quote_ho_model.id, :full_quote_ho_claim_id => claim.id}, :update => {:success => 'claim', :failure => 'notice'}, :html => {:style => "background: red"}, :confirm => 'Are you sure you wish to remove this claim?' %></td> 22. </tr> 23. <% end %> 24. <% end %> 25. </table> 26. <% end %> 27. </div>
The controller code is relatively the same for all actions, and I'm experiencing the problem with all of them once I submit an AJAX request, no matter which one it is. The thing is, the partial is rendered with the same state in the initial view as it is after any AJAX requests.
And the weirdest part is, if I move the <% end %> tag from the end of the top half of the partial to the end of the bottom half of the partial (extending the remote_form_for to the end of the partial instead of just to the form part) then the topmost record does show up. Does anyone have any idea what's going on?
Just for reference, here's the controller code:
1. class FullQuoteHoLocsController < ApplicationController 2. before_filter :setupFullQuoteHoModel 3. layout "fqho" 4. 5. def setupFullQuoteHoModel 6. @full_quote_ho_model = FullQuoteHoModel.find(params[:full_quote_ho_model_id]) 7. @full_quote_ho_loc = FullQuoteHoLoc.find(params[:id]) 8. end 9. 10. def variables_from_params(modify) 11. if params[:full_quote_ho_claim_id] != nil 12. @full_quote_ho_claim = FullQuoteHoClaim.find(params[:full_quote_ho_claim_id]) 13. else 14. @full_quote_ho_claim = FullQuoteHoClaim.new 15. end 16. @modify = modify 17. 18. refresh_claims() 19. end 20. 21. def refresh_claims 22. @full_quote_ho_claims = FullQuoteHoClaim.find(:all, :order => :id, :conditions => {:full_quote_ho_loc_id => @full_quote_ho_loc.id}) 23. if @full_quote_ho_claims.size > 0 24. flash[:notice] = nil 25. else 26. @full_quote_ho_claims = nil 27. flash[:notice] = 'No claims exist for this quote.' 28. end 29. return @full_quote_ho_claims 30. end 31. 32. def add_claim 33. controllerDropDownsSetup() 34. variables_from_params(false) 35. 36. @claim = FullQuoteHoClaim.create({ 37. :description => params[:full_quote_ho_claim][:description], 38. :cause_of_loss => params[:full_quote_ho_claim][:cause_of_loss], 39. :date_of_loss => params[:full_quote_ho_claim][:date_of_loss], 40. :amount_of_loss => params[:full_quote_ho_claim][:amount_of_loss] 41. }) 42. @full_quote_ho_loc.full_quote_ho_claims << @claim 43. 44. render :partial => 'claim', :layout => false 45. end 46. 47. def edit_claim 48. controllerDropDownsSetup() 49. variables_from_params(true) 50. 51. @full_quote_ho_claims = refresh_claims() 52. render :partial => 'claim', :layout => false 53. end 54. 55. def update_claim 56. controllerDropDownsSetup() 57. variables_from_params(false) 58. 59. @full_quote_ho_claim.update_attributes(params[:full_quote_ho_claim]) 60. @full_quote_ho_claim = FullQuoteHoClaim.new 61. 62. render :partial => 'claim', :layout => true 63. end 64. 65. def remove_claim 66. controllerDropDownsSetup() 67. variables_from_params() 68. 69. @full_quote_ho_loc.full_quote_ho_claims.find(@full_quote_ho_claim).destroy 70. @full_quote_ho_claim = FullQuoteHoClaim.new 71. 72. render :partial => 'claim', :layout => false 73. end 74. 75. ... 76. 77. # GET /full_quote_ho_locs/1/edit 78. def edit 79. controllerDropDownsSetup() 80. variables_from_params(false) 81. end
As you can see, the edit is initialized the same way that all the others are. When I first go to the edit view, the form and display show up perfectly, with all records listed. The partial is rendered completely. However, when I fire any of the AJAX buttons, the partial is only half-way rendered, showing the form, but not the listing of all records. Does anyone know why this could be the case?
Thanks in advance, and for taking the time to read, Ahad.