Rails 'nil' 不是与 ActiveModel 兼容的对象.它必须实现:to_partial_path.阿贾克斯
Rails 'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path. ajax
我正在按照第 4 节(服务器端问题)在页面上设置 ajax。 我已经完全复制了教程文本(用我自己的模型名称替换模型名称),它会创建并保存我的"参与者"记录,但不会自动刷新 ajax 部分。
这是我得到的错误...看起来它指的是我的创建.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
。
你已经在JS中注释掉了这一行,但是ERB仍然将由Rails处理,所以它仍然试图做render @participants
更新
为了未来...该错误的最后一行是关键:
app/views/participants/create.js.erb:2
请参阅最后的2
,它告诉您错误发生在哪一行,因此这是查找问题时需要关注的地方。
我能够修复此错误。我错过了在控制器中创建操作。
所以在你的例子中,我会错过控制器中的创建操作。因此,当使用该操作测试任何内容时,它将失败。
任何将来遇到此问题的用户,请确保已定义操作,并且在控制器级别没有错误/拼写错误。
相关文章:
- 如何使用动画实现纸张推车
- 客户端服务器REST API captcha实现
- 如何实现此布局
- Meteor忘记了密码的实现
- 在ejs-partial中对JSON对象进行迭代
- 使用Native Sockets在Android中实现WebSockets
- 在样板文件中实现Ajax
- instanceof是如何在JavaScript中实现的
- 如何正确实现Jquery多选小部件
- 实现一个建立在google.com之上的自定义搜索引擎
- 多个组件是如何实现的
- window.location使用jquery mobile实现chrome跳转
- 如何在OpenERP中实现网络摄像头
- Node.js使用Series函数(模式?)实现流控制时出现意外结果
- javascript加密实现,包括可信否认
- 实现比较方法的最佳实践是什么;s的比较类型是在运行时选择的
- 如何让程序员在javascript中实现正确的回调
- 如何使用自定义辅助对象(handler)实现嵌套的每个循环
- AngularJS智能表全局配置实现
- Expressjs/AngularJS:实现req-flash后出错