我有一个名为events\new.html.erb的视图,它包含其他用户的集合选择。我想让它使得每次用户在集合select中选择一个不同的名字时,都会用所选用户的Calendar填充一个<div id="colleaguecal"></div>,其中包含他们的个人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'然后在我的Calendars_controller#calendarChange中使用javascript var colleagueID
#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' %>");请帮帮我!谢谢
发布于 2016-01-21 20:52:24
参考这一点,请注意,在渲染部分时不需要给出_(下划线)。
$('.element').html("<%= escape_javascript(render :partial => 'calendars/showColleague') %>");https://stackoverflow.com/questions/34924185
复制相似问题