[英]AJAX not rendering partial
我有一個名為events\\new.html.erb
的視圖,其中包含其他用戶的精選集合。 我要這樣做,以便每次用戶在集合選擇中選擇一個不同的名稱時, <div id="colleaguecal"></div>
就會被選定的用戶Calendar
保存,該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
我需要渲染events\\new.html.erb
中的calendars\\_showColleague.html.erb
部分,其中_showColleague
傳遞了在calendars#calendarChange中創建的@events
和@colcalendar
變量。 在客戶與collection_select進行交互之前,那里應該沒有任何內容,然后一旦他們選擇了另一個客戶,則所選客戶的日歷(_showColleague)應該顯示在下面。 我該怎么做?
昨天我被告知要這樣做,但是它沒有渲染任何東西:
#app/views/calendars/new.js.erb
$(".element").html("<%=j render 'calendars/_showColleague' %>");
請幫忙! 謝謝
請參考此內容,請注意在渲染局部圖像時無需提供_(下划線)。
$('.element').html("<%= escape_javascript(render :partial => 'calendars/showColleague') %>");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.