vendredi 27 octobre 2017

Rails - Refreshing ajax partial that contains nested array returns undefined method error

I have two models;

class Course < ActiveRecord::Base
  has_permalink :name, :unique => true

  has_many :courselocations
  accepts_nested_attributes_for :courselocations
end

class Courselocation < ActiveRecord::Base
  has_permalink :urlname, :unique => true

  belongs_to :courses
end

In my Course > Edit view, I'm calling a partial that loops through all the associated courselocations for that course. The user can edit each courselocation and update via ajax.

Admin > Courses > Locations Partial

  <div class="row marginBottom40">
    <% @course.courselocations.each do |location| %>
    <div class="padAll20 ghostBorder borderBox col-lg-3 colMarginRight whiteBG col">
      <h3 class="marginBottom10"><%= location.name %></h3>
      <div class="small-text"><%= location.city %>, <%= location.state %></div>
      <ul class="marginTop20 stretch"  style="    flex: 1 0 auto;">
        <li><h4 class="marginBottom10">Instructors</h4></li>
        <% for s in location.coursesessions %>
          <% if s.courseinstructor_id.nil? %>
            <p>Add instructors by <%= link_to "managing", "/admin/courses/#{@course.permalink}/courselocations/#{location.permalink}/edit", :class => 'displayBlock', :remote => true, :id => "#{location.id}_link" %> the course location</p>
          <% else %>
            <li class="marginBottom20 teachers row">
              <div class="col-lg-3 selfCenter">
                <%= cl_image_tag s.courseinstructor.image_url(:profileSmall), :class => "image" %>
              </div>
              <div class="col-lg-9">
                <%= s.courseinstructor.first_name %> <%= s.courseinstructor.last_name %>
                <div class="small-text"><%= s.courseinstructor.title %></div>
                <div class="small-text"><%= s.courseinstructor.company %></div>
              </div>
            </li>
          <% end %>
        <% end %>
        <li><h4 class="marginBottom20">Dates</h4></li>
        <li>
          <% for d in location.coursedates %>
            <span class="padAll10 grayBG inlineBlock"><%= d.start.strftime('%b %d, %y') %></span>
          <% end %>
        </li>
      </ul>
      <div class="padTop20 col-lg-end">
        <%= link_to "Manage", "/admin/courses/#{@course.permalink}/courselocations/#{location.permalink}/edit", :class => 'block btn btn-outline', :remote => true, :id => "#{location.id}_link" %>
      </div>
    </div>
    <% end %>
  </div>

Admin > Courselocations > Update.js.erb

$(document).ajaxSuccess(function(event, request) {
  $("#edit_location").empty();
  $('#locationData').slideDown('slow').after('');
});
$('#locations').html("<%= j render(partial: '/admin/courses/locations', locals: {location: @course.courselocations}) %>");

The problem however is that on update, I get a NoMethodError in Admin::Courselocations#update ... undefined method 'courselocations' for nil:NilClass

I'm assuming that since I'm calling the edit view remotely from courselocation that Rails doesn't understand what @course.courselocations is but I'm not sure how to solve this issue.

Aucun commentaire:

Enregistrer un commentaire