Javascript 如何使用AJAX呈现html.erb部分

Javascript 如何使用AJAX呈现html.erb部分,javascript,jquery,ruby-on-rails,ruby,ajax,Javascript,Jquery,Ruby On Rails,Ruby,Ajax,我是编程新手,我不知道如何处理这个逻辑: 我在我的events\new.html.erb视图中有一个集合选择,其中客户可以选择另一个客户: #events\new.html.erb <%= f.collection_select :id, Customer.where(business_id: current_customer.business_id),

我是编程新手,我不知道如何处理这个逻辑:

我在我的
events\new.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)" } %>


            <div id = colleaguecal>

            </div>
该变量在日历#calendarChange:

def calendarChange
    colleagueID = params[:colleagueID]
    @colleague = Customer.where(id: :colleagueID)

    @colcalendar = @colleague.calendar
    @events = @colleague.calendar.events #sets the variables I need to use for the customer's calendar (_calendarChange.html.erb) that needs to be rendered within events\html.erb

  end
我如何使其在更改集合选择时,使用日历中定义的
@events
变量在我的
events\html.erb
中呈现我的
日历\\u calendarChange

谢谢

编辑:

为了明确我想要实现的目标:


我希望客户能够在collection_select中选择另一个客户,并在同一页面上查看所选客户的日历。此日历需要
@events
才能对所选客户唯一。(每个客户都有自己的日历和一系列活动)

如果我了解您想要实现的目标:

ruby
/
rails
world中,作为一种惯例,我们使用snake-case命名方法和变量。因此这里的
calendarChange
将变成
calendar\u change

当调用函数
renderColCal
时,它将点击
calendar\u change
方法

您需要在这里创建一个
calendars/calendar\u change.js.erb
,作为响应提供
javascript
功能

日历/calendar_change.js.erb
//把你想要的javascript放在这里
//您还可以使用ruby代码,将其包装为erb标记
//这里有一个例子:
$(“#包装器”).html(“”)

更多信息。

好的,但如何使其在events\new.html.erb中呈现?我不希望用户被发送到单独的页面,因为这只是他们必须在events\new.html.erb中填写的表单的一小部分。这已经为我安排好了吗?我只想让它出现在“collaguecal”的div中,你说“你需要在这里创建日历/calendar\u change/.js.erb”,你是说respond\u to do | format | format.js{}end你不需要
respond\u to
来让它工作。尝试将
calendar\u change.js.erb
的内容替换为
alert('hello')
,并检查它是否工作。它给了我:HTTP404:NOT FOUND-服务器没有找到任何与请求的URI(统一资源标识符)匹配的内容。(XHR):获取-http://127.0.0.1:3000/calendars\u controller/calendar\u change?collagueid=3Did您有一个与
calendar\u change匹配的路由
def calendarChange
    colleagueID = params[:colleagueID]
    @colleague = Customer.where(id: :colleagueID)

    @colcalendar = @colleague.calendar
    @events = @colleague.calendar.events #sets the variables I need to use for the customer's calendar (_calendarChange.html.erb) that needs to be rendered within events\html.erb

  end
// put here whatever javascript you want
// You could also use ruby code by wrapping it with erb tags
// Here an example:
$("#wrapper").html("<%= escape_javascript(render @events ) %>")