Rails 'nil' 不是 ActiveModel 兼容的对象。它必须实现:to_partial_path。阿贾克斯

Not*_*ere 5 javascript forms ajax ruby-on-rails ruby-on-rails-4

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

\n\n

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

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

这是我的代码

\n\n
class ParticipantsController < ApplicationController\ndef new\n  @participant = Participant.new\n  @participants = @participants.recently_updated\nend\n\ndef create\n  @participant = Participant.new(participant_params)\n\n  respond_to do |format|\n    if @participant.save\n      format.html { redirect_to @participant, notice: \'Helper Invited!\' }\n      format.js   {}\n      format.json { render json: @participant, status: :created, location: @participant }\n    else\n      format.html { render action: "new" }\n      format.json { render json: @participant.errors, status: :unprocessable_entity }\n    end\n  end\nend\n
Run Code Online (Sandbox Code Playgroud)\n\n

_form.html.erb

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

_participant.html.erb

\n\n
<li >\n<%= participant.email %> <%= link_to participant, remote: true, method: :delete,  data: { confirm: \'Are you sure?\' } do %>REMOVE<% end %>\n</li>\n
Run Code Online (Sandbox Code Playgroud)\n\n

创建.js.erb

\n\n
$("<%= escape_javascript(render @participant) %>").appendTo("#participants");\n
Run Code Online (Sandbox Code Playgroud)\n\n

销毁.js.erb

\n\n
$(\'#participants\').html("<%= j (render @participants) %>");\n
Run Code Online (Sandbox Code Playgroud)\n

sma*_*thy 5

它位于create.js.erb文件的第 2 行,缺少的@participants@participant.

您已经在 J​​S 中注释了该行,但 ERB 仍将由 Rails 处理,因此它仍在尝试执行以下操作render @participants

更新

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

app/views/participants/create.js.erb:2
Run Code Online (Sandbox Code Playgroud)

请参阅2最后,它告诉您错误发生在哪一行,因此这是查找问题时需要关注的地方。