简体   繁体   English

Rails 4 Ajax NoMethodError-我的create.js.erb文件中nil:NilClass:的未定义方法'id'

[英]Rails 4 Ajax NoMethodError - undefined method `id' for nil:NilClass: in my create.js.erb file

I'm trying to render a partial when a button is clicked. 我试图在单击按钮时渲染局部图像。 I believe I'm doing something obviously wrong... and I expect it's not going to be in the following: 我相信我做的事情显然是错误的...而且我希望不会出现以下情况:

$('#modrequest').empty();
$('#modrequest').html("<%= escape_javascript(render(:partial => 'moderator_requests/links')).html_safe %>");

The error is 错误是

NoMethodError - undefined method `id' for nil:NilClass: NoMethodError-未定义的方法id为nil:NilClass:

I expect I'm doing something wrong elsewhere, but I'm not sure where, so instead of posting a ton of code, I'll post any file requested. 我希望我在其他地方做错了,但是我不确定在哪里,所以我将发布任何请求的文件,而不是发布大量代码。

Thanks 谢谢

Error 错误

The error will likely be a result of you calling unset data in your partial : 该错误很可能是由于您在partial调用未设置的数据导致的:

#partial
<%= item.id %>

-- -

Fix 固定

Because you have to pass local variables to your partials , you're not passing any - which will prevent it from calling the data it needs. 因为您必须将局部变量传递给parts ,所以您没有传递任何变量 -这将阻止它调用所需的数据。 You'll be able to resolve the issue like this: 您将可以解决以下问题:

$('#modrequest').html("<%=j render(:partial => 'moderator_requests/links', locals: {your: "local"}).html_safe %>");

If your data is not able to be passed to your partial, you'll be much better putting it into a helper , which will allow you to populate it in any part of your app 如果您的数据无法传递到部分数据,则最好将其放入helper ,这将允许您将其填充到应用程序的任何部分

-- -

Definition 定义

NoMethodError - undefined method `id' for nil:NilClass NoMethodError-未定义的方法'id'为nil:NilClass

This error basically means you're trying to call id on a non-existent piece of data. 此错误基本上意味着您正在尝试对不存在的数据段调用id You'll either need to show the partial, or determine which data you're trying to call in the partial 您要么需要显示部分数据,要么确定要在部分数据中调用的数据

To fix this, you basically need to be able to pass the right data to your partial; 为了解决这个问题,您基本上需要能够将正确的数据传递给您的局部数据。 either by way of local variables, or with a helper 通过local变量或使用helper

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails 4 / ajax-NoMethodError-未定义的方法&#39;id&#39;为nil:NilClass-将变量传递给js模态 - Rails 4/ ajax - NoMethodError - undefined method `id' for nil:NilClass - Passing variable to js modal 如何在create.js.erb文件中测试哪个页面发送了AJAX请求? - How can I test which page sent an AJAX request in my create.js.erb file? 为什么我的create.js.erb无法呈现? - Why is my create.js.erb not rendering? Rails 4,如何在create.js.erb中执行控制器特定的操作 - Rails 4, how to do a controller specific action in create.js.erb 如何在我的create.js.erb中重置/清除表单? - How to reset/clear form within my create.js.erb? Rails为nil:NilClass创建动作未定义的方法“ each” - Rails create action undefined method 'each' for nil:NilClass Create.js.erb Javascript要求部分 - Create.js.erb Javascript asking for partial 如何解决“ NoMethodError(nil:NilClass的未定义方法[]”):应用程序/控制器/…” - How to fix “NoMethodError (undefined method []' for nil:NilClass): app/controllers/…” ReservationsController#checkout中nil:NilClass NoMethodError的未定义方法&#39;total&#39; - undefined method `total' for nil:NilClass NoMethodError in ReservationsController#checkout nil:NilClass rails 4的未定义方法`category` - undefined method `category` for nil:NilClass rails 4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM