I have this if statement in my video show view:
<% if @video.user == current_user && current_user != nil && @video.comment_titles.count < 3 %>
<%= link_to "Add Comment Title", "#", :id => "comment_title_link", :class => "edit" %>
<%= simple_form_for @video, :remote => true do |f| %>
<%= f.input :comment_title_names, :label => false, :placeholder => "Add a Comments Title" %>
<%= f.button :submit, :value => 'Add', :id => 'add_comment_title' %>
<div class='hint'>Let your listeners know what comments you want by adding a guiding title for them. Pose a question, ask for feedback, or anything else!</div>
<% end %>
<% elsif @video.comment_titles.count == 3 && @video.user == current_user && current_user != nil %>
<p> You have reached your limit of comment titles. You can always add a new one by deleting one of your old ones. </p>
<% end %>
This if statement essentially evaluates @video.comment_titles.count to determine if the if statement or the elsif statement is true. I let users add comment_titles with ajax and so by the time @video.comment_titles.count == 3 is true, it won’t correctly evaluate the if statement since the if statement, which is in my video show view, is only called after a page reload.
I want the if statement to be called dynamically every time the number of comment_titles changes, which is equivalent to saying whenever the AJAX call for updating comment_titles is triggered. However, I’d rather do this on the client side than have to do it in a .js.erb file. How would I trigger this on the client side?
OK so no one has answered, so I’m assuming either I have not provided enough code, I have not been clear in what I am trying to do, or it is impossible. Which is it?
After reading your question again with your comment in mind, I assume that
<div id="add_comment_title_action">fragment</div>$('#add_comment_title_action').load('fragment_url')but you rather just do it within the clientLet’s assume that the event handler looks something like
Then you could have a function for updating the fragment
How the code can be made better:
.html(...)then the event handlers for the elements within the fragment should be bound with.live(event, handler)so that you can enable adding new comment titles after a delete has been performedNote that every check done in the client side must be done in the server side also.
See history for completely different answer 🙂