Skip to content Skip to sidebar Skip to footer

Want To Evaluate An If Statement Every Time An Ajax Call For Creating Comment_titles Is Triggered

I have this if statement in my video show view: <% if @video.user == current_user && current_user != nil && @video.comment_titles.count < 3 %><%= l

Solution 1:

After reading your question again with your comment in mind, I assume that

  • There is a javascript event handler for the event 'new comment title has been added'.
  • The provided fragment resides withing a container of sort e.g.<div id="add_comment_title_action">fragment</div>
  • The fragment should be refreshed every time the event handler is executed
  • You've seen that is possible to load the fragment by $('#add_comment_title_action').load('fragment_url') but you rather just do it within the client

Let's assume that the event handler looks something like

$('#add_comment_title_form').submit(function(e) {
   e.preventDefault();
   $.post( 'url',
          { 'title': 'Lorem ispum dolor sit amet'}
   );
   updateFragment();       
});

Then you could have a function for updating the fragment

updateFragment() {
  // Let's assume that @video.user == current_user && !current_user.nil?if($('.comment_title').size() < 3) {
     // NOP add something here to handle case when a title is deleted
  } else {
     $('#add_comment_title_action').html('<p> You have reached your limit of comment titles. You can always add a new one by deleting one of your old ones. </p>'); 
  }
}

How the code can be made better:

  • There should be some callback to check if the post request has succeeded and proceed accordingly
  • You could handle the two different states of the fragment by simply hiding and showing the options depending on the state (if you can assume all the users have enabled javascript and css)
  • If you decide to replace the fragment with .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 performed

Note that every check done in the client side must be done in the server side also.

See history for completely different answer :)

Solution 2:

I kinda get what you mean by now. You should always sync your frontend with your backend so I suggest you do an ajax request when you submit the comment. Your request should return true or false if the user can comment and if he can, post it and reload that partial. I'm not a fan or rjs(which you seem to be using) so I wouldn't know the syntax but if it was jquery I could provide you with a more detailed answer.

anyway, i'll do this in bullet points

  • user submits form through ajax

    $("#add_comment_title").click( function(){ $.ajax({ type: 'POST', data: $("#formwhatever").serialize(), complete: function(html) { $("#divthatcontainsthatwholeifstatement").replaceWith(html); } }) })

  • backend checks if user has enough posts already. if false, return false to browser. if true, add to db and render partial

    in your controller:

    def create #pseudo code since im also working lol if user.comments.size < 3 #save comment watever end

    @comments = updated comments

    respond_to do |format| format.js { render :partial => "partialthatcontainsthatif" } end

    end

  • ???
  • PROFIT

this "refreshes" the partial no matter what. if the user already had more than 3 comments then it will return and show that error check you have

Post a Comment for "Want To Evaluate An If Statement Every Time An Ajax Call For Creating Comment_titles Is Triggered"