【发布时间】:2023-03-05 05:02:01
【问题描述】:
我有一个名为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
我需要在 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' %>");
请帮忙!谢谢
【问题讨论】:
标签: javascript jquery html ruby-on-rails ajax