javascript - Rails 'nil' 不是与 ActiveModel 兼容的对象。它必须实现 :to_partial_path. ajax

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

我正在按照第 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>

创建.js.erb

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

销毁.js.erb

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

最佳答案

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

您已经在 J​​S 中注释掉了这一行,但 ERB 仍将由 Rails 处理,因此它仍在尝试执行 render @participants

更新

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

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

看到最后的2,它告诉你错误发生在哪一行,所以这是你在寻找问题时需要关注的地方。

关于javascript - Rails 'nil' 不是与 ActiveModel 兼容的对象。它必须实现 :to_partial_path. ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29013156/

相关文章:

javascript - 工具提示内容未定义 - 谷歌地图

javascript - 从数组中的字符串中删除数字

javascript - 我可以在 Ruby on Rails 中使用 JavaScript 部分吗? Partial 正在尝试以 HTML 形式呈现

ruby-on-rails - RubyGem 版本错误 : rack(1. 0.0 不是 ~> 1.0.1)

javascript - 如何提高 Fabric.Image.fromURL 的性能

php - PHP 中的哪些代码片段可以创建一个输入表单,从而在我的 mysql 数据库中创建一组新数据?

ruby-on-rails - Rails 5 升级 :/actionpack-5. 0.0/lib/action_controller/test_case.rb:49:in `initialize':参数数量错误(0 代表 2)(ArgumentError)

java - 如何处理由于 WebSphere 中的多个 AJAX 请求而导致的高线程使用率

javascript - 将元素列表传递给循环以显示内容时,但id始终返回最后一个元素

javascript - js在ajax本身回调后停止点击函数后续执行