我有两个型号;
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
在我的课程中&gt;编辑视图,我正在调用部分循环遍历该课程的所有相关联的courselocations。用户可以编辑每个courselocation并通过ajax进行更新。
管理员&gt;课程&gt;地点部分
<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>
管理员&gt; Courselocations&gt; 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}) %>");
问题是,在更新时,我得到NoMethodError in Admin::Courselocations#update
... undefined method 'courselocations' for nil:NilClass
我假设因为我从courselocation
远程调用编辑视图,Rails不理解@course.courselocations
是什么,但我不知道如何解决这个问题。