AJAX不呈现部分

时间:2016-01-21 12:43:10

标签: javascript jquery html ruby-on-rails ajax

我有一个名为events\new.html.erb的视图,其中包含其他用户的集合选择。我希望这样做,以便每次用户在集合选择中选择其他名称时,<div id="colleaguecal"></div>都会填充所选用户Calendar,其中包含他们的个人Events 。但是,我似乎无法获得包含我想要呈现的日历的部分calendars\_showColleague.html.erb,以便在更改集合选择时实际显示...

#events\new.html.erb

<%= f.collection_select :id,
                                Customer.where(business_id: current_customer.business_id),
                                :id,
                                :full_name,
                                { prompt: 'Select' },
                                { id: "colleageselect", onChange: "renderColCal(this)" } %>
 
#application.js
function renderColCal(select){

    var colleagueID = select.value ; //this variable is sent to Ruby for the database query to find the selected user
    $(document).on("change", "select#id", function(e){

            $.get("customers/"+ +$(this).val() + "/calendars/1");
        }
    );

}

...

#routes.rb
post 'calendars_controller/calendarChange', to: 'calendars_controller#calendarChange'

然后我正在使用javascript var colleagueID并在Calendars_controller#calendarChange中使用它:

#calendars_controller.rb
class CalendarsController < ApplicationController
  respond_to :js, :html


  def new
    @calendar = Calendar.new(calendar_params)

  end

  def create
    @calendar = Calendar.new(calendar_params)
  end

private
  def calendar_params
    params.require(:customer_id)
  end


  def show
    @calendar = current_customer.calendar
    @events = @calendar.events
  end

  def calendarChange
    colleagueID = params[:colleagueID] #the JS var is taken in here and then used to find the user in the database
    @colleague = Customer.where(id: :colleagueID)

    @colcalendar = @colleague.calendar
    @events = @colleague.calendar.events #the specific events that I want to render to the '_showColleague.html.erb' template

  end

end

我需要在calendars\_showColleague.html.erb内呈现部分events\new.html.erb,其中_showColleague传递了在日历#calendarChange中创建的@events@colcalendar变量。在客户与collection_select进行交互之前,应该没有任何内容,然后一旦他们选择了另一个客户,所选客户的日历(_showColleague)应该出现在下面。我该怎么做?

昨天我被告知这样做,但它没有呈现任何东西:

#app/views/calendars/new.js.erb
$(".element").html("<%=j render 'calendars/_showColleague' %>");

请帮忙!感谢

1 个答案:

答案 0 :(得分:0)

请参阅此处,请注意在渲染部分时不需要给出_(下划线)。

  $('.element').html("<%= escape_javascript(render :partial => 'calendars/showColleague') %>");