【问题标题】:Rails 'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path. ajaxRails 'nil' 不是 ActiveModel 兼容的对象。它必须实现 :to_partial_path。阿贾克斯
【发布时间】:2015-05-14 19:25:03
【问题描述】:

我正在按照第 4 节(服务器端问题)在页面上设置 ajax。我已经完全复制了教程文本(用我自己的替换模型名称),它创建并保存了我的“参与者”记录,但不会自动刷新 ajax 部分。

这是我得到的错误...看起来它指的是我的 create.js.erb

ActionView::Template::Error ('nil' is not an ActiveModel-compatible object. It must implement :to_partial_path.):
    1: $("<%= escape_javascript(render @participant) %>").appendTo("#participants");
    2: // $('#participants').html("<%= j (render @participants) %>");
  app/views/participants/create.js.erb:2:in `_app_views_participants_create_js_erb___1675277149181037111_70181034249880'

这是我的代码

class ParticipantsController < ApplicationController
def new
  @participant = Participant.new
  @participants = @participants.recently_updated
end

def create
  @participant = Participant.new(participant_params)

  respond_to do |format|
    if @participant.save
      format.html { redirect_to @participant, notice: 'Helper Invited!' }
      format.js   {}
      format.json { render json: @participant, status: :created, location: @participant }
    else
      format.html { render action: "new" }
      format.json { render json: @participant.errors, status: :unprocessable_entity }
    end
  end
end

_form.html.erb

<ul id="participants">
  <%= render @participants %>
 </ul>

<%= form_for(@participant, remote: true) do |f| %>
  <%= f.label :email %><br>
  <%= f.email_field :email %>
<%= f.submit 'SUBMIT' %> 
 <script>
  $(document).ready(function() {
  return $("#new_participant").on("ajax:success", function(e, data, status, xhr) {
    return $("#new_participant").append(xhr.responseText);
  }).on("ajax:error", function(e, xhr, status, error) {
    return $("#new_participant").append("<p>Oops.  Please Try again.</p>");
  });
});
 </script>
 <script>
$(function() {
  return $("a[data-remote]").on("ajax:success", function(e, data, status, xhr) {
    return alert("The helper has been removed and notified.");
  });
});
</script>

_participant.html.erb

<li >
<%= participant.email %> <%= link_to participant, remote: true, method: :delete,  data: { confirm: 'Are you sure?' } do %>REMOVE<% end %>
</li>

create.js.erb

$("<%= escape_javascript(render @participant) %>").appendTo("#participants");

destroy.js.erb

$('#participants').html("<%= j (render @participants) %>");

【问题讨论】:

  • 检查您是否在create.js.erb 文件中获得@participant

标签: javascript ruby-on-rails ajax forms ruby-on-rails-4


【解决方案1】:

它在您的 create.js.erb 文件的第 2 行,它是缺少的 @participants 而不是 @participant

您已经在 J​​S 中注释掉了这行代码,但是 ERB 仍将由 Rails 处理,所以它仍在尝试执行render @participants

更新

对于未来...关键在于该错误的最后一行:

app/views/participants/create.js.erb:2

查看末尾的2,它告诉您错误发生在哪一行,因此您在查找问题时需要重点关注这一点。

【讨论】:

  • 这工作....意味着它刷新并显示新记录,但它把这个错误放在刚刚使用的形式下:$(" \n\n :sample@email.com \n 删除\n\n \n\n\n").appendTo("#participants");
  • 所以你需要接受我的回答来解决这个问题,如果需要的话,再问一个关于你的新问题的新问题。
  • 我已经发布了第二个问题。 stackoverflow.com/questions/29034208/…
猜你喜欢
  • 2018-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-11
  • 1970-01-01
  • 1970-01-01
  • 2016-05-09
  • 2018-05-15
相关资源
最近更新 更多