我有一個名爲events\new.html.erb
的視圖,其中包含其他用戶的集合選擇。我想這樣做以便每當用戶在收集選擇中選擇一個不同的名稱時,<div id="colleaguecal"></div>
就會填滿所選用戶的Calendar
,該用戶擁有他們的個人Events
。不過,我似乎無法得到部分calendars\_showColleague.html.erb
持有我想渲染,實際上出現在集合中選擇改變日曆...AJAX不呈現部分
#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變種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
通過在日曆#calendarChange中創建的變量@events
和@colcalendar
。在客戶與collection_select進行交互之前,應該沒有任何內容,然後一旦他們選擇了另一個客戶,所選客戶的日曆(_showColleague)應顯示在下面。我將如何做到這一點?
有人告訴我,這個昨天做的,但它沒有任何渲染:
#app/views/calendars/new.js.erb
$(".element").html("<%=j render 'calendars/_showColleague' %>");
請幫幫忙!謝謝
好的謝謝,這並沒有解決我的問題,但是 –