1

Rails 3.2 jQuery-Rails

I've searched all over and I've only found js ways to update a partial or add a partial via JS. I've had no luck with this one. Here is the issue:

I have a list of referrals on a page, the referrals are posted on the index page as partials. The referral box has a reply button and a count of how many replies per referral. Statically I had it working but I'm getting no luck with updating the referral box with the new count and replacing the reply button with an icon. The is getting to the database but I'm screwing something up with the refresh of the box. Help please!

  1. Do I use locals to render the new variables?
  2. Should I remove my if statements from the partial and just use the js.erb file to determine what to show?

Reply controller:

class RepliesController < ApplicationController
  respond_to :html, :js
  def new
    @referral = Referral.find(params[:referral_id])
    @reply = @referral.replies.new(:user_id => params[:user_id], :referral_id => params[:referral_id])
    if @reply.save
      format.html { notice: 'Replied' }
      respond_with(@referral, :location => referrals_path)
    end
  end
end

_referral partial:

.referralBox.cornerShadow{:id => "Referral#{referral.id}"}
  - if current_user == referral.user or current_user.role == 'administrator'
    = link_to '<i class="icon-pencil icon-large icon-grey" rel="tooltip" title="Edit"></i>'.html_safe, edit_referral_path(referral), class: "referralEdit"
    = link_to '<i class="icon-remove icon-large icon-grey" rel="tooltip" title="Delete"></i>'.html_safe, referral, method: :delete, data: { confirm: 'Are you sure?' }, class: "referralClose"
  .referralProfile
    = link_to "#{image_tag(referral.user.image.url, :class => "img-circle img-border border-white", :style => "thumb")}".html_safe, referral.user

  %ul.unstyled.inline
    %li
      = referral.description
  - unless referral.replies.count == 0
    - if referral.user == current_user
      %i.icon-comments.icon-large{"rel" => "tooltip", "title" => "Replies"}
        = referral.replies.count
    - else
      %i.icon-comments.icon-large.orange{"rel" => "tooltip", "title" => "Replies"}
        = referral.replies.count
  - unless current_user == referral.user
    - if referral.replies.any?
      %i.icon-ok.icon-large.pull-right.orange{"rel" => "tooltip", "title" => "Replied"}
    - else
      = link_to '<i class="icon-ok icon-large pull-right icon-grey" rel="tooltip" title="Reply"> Reply</i>'.html_safe, new_referral_reply_path(:referral_id => referral.id, :user_id => current_user.id), :id => "ReplySubmit", :remote => true

new.js.erb view

$(".referralBox").html("<%= j(render(:partial => "@referral")) %>");

application.js

jQuery.ajaxSetup({
    'beforeSend': function (xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
});
4

0 回答 0