简体   繁体   中英

AJAX not rendering partial

I have a view called events\\new.html.erb which holds a collection select of other users. I want to make it so that every time the user selects a different name in the collection select, a <div id="colleaguecal"></div> is filled with the selected user's Calendar which holds their personal Events . However, I can't seem to get the partial calendars\\_showColleague.html.erb which holds the calendar I want to render, to actually appear when the collection select is changed...

#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'

Then I am taking the javascript var colleagueID and using it in my 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

I need to render the partial calendars\\_showColleague.html.erb within events\\new.html.erb where _showColleague is passed the @events and @colcalendar variables created in calendars#calendarChange. Before a customer has interacted with the collection_select, there should be nothing there, then once they have selected another customer, the selected customer's calendar (_showColleague) should appear below. How would I do this?

I was told to do this yesterday but it is not rendering anything:

#app/views/calendars/new.js.erb
$(".element").html("<%=j render 'calendars/_showColleague' %>");

Please help! Thanks

请参考此内容,请注意在渲染局部图像时无需提供_(下划线)。

  $('.element').html("<%= escape_javascript(render :partial => 'calendars/showColleague') %>");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM