将变量传递给局部变量或方法错误

时间:2022-10-08 23:54:09

Here is the code in my view to call the partial:

这是我认为调用部分的代码:

<%= render(:partial => "tabs", :locals => {:class_Name => "Science", :y => 36}) %>

and now here is what's in _tabs.html.erb:

这里是_tab。html。erb:

<div>
<h1> <%= class_Name %> </h1>
</div>

I expect HTML output of:

我期望HTML输出:

<div>
<h1> Science </h1>
</div>

But instead I get the error:

但是相反,我得到了错误:

undefined local variable or method `class_Name' for #<#<Class:0x007f873b156c28>:0x007f873b1f9540>

I have closed and restarted Aptana (the IDE I use), and restarted the server multiple times

我已经关闭并重启了Aptana(我使用的IDE),并多次重启服务器

Thanks in advance for your time.

提前谢谢你的时间。

4 个解决方案

#1


50  

I've noticed that if I did not include 'partial:' before my partial path, like so:

我注意到,如果在我的部分路径之前没有包含“部分:”,就像这样:

<%= render 'my_partial', :locals => {:class_Name => "Science", :y => 36} %>

I was required to use the hash+symbol in my partial to access the desired values as others have noted.

我被要求在我的部分中使用散列+符号来访问所需的值。

<div>
<h1> <%= locals[:class_Name] %> has a y value of <%= locals[:y] %></h1>
</div>

However, including 'partial:' before my partial path:

但是,包括“部分:”在我的部分路径之前:

<% render partial: 'my_partial', :locals => {:class_Name => 'Science', :y => 36 } %>

...allowed me to simply call the hash values directly.

…允许我直接调用哈希值。

<div>
<h1><%= class_Name %> has a y value of <%= y %></h1>
</div>

Just something to keep in mind, this issue tripped me up for awhile when trying to send the locals hash to my partial.

需要记住的是,这个问题让我有一段时间都搞砸了。

#2


19  

If you are using the term partial: in your render, for example like so:

如果你使用的是局部:在你的渲染中,例如:

<% render partial: 'your_partial', locals: {foo: 'Biggs', bar: 'Wedge'} %>

<%渲染部分:'your_partial', local: {foo: 'Biggs', bar: 'Wedge'} %>。

Then your locals will be available simply as foo and bar, directly accessibly thus:

然后,你的本地用户将以foo和bar的形式提供,直接可访问如下:

<%= foo %> //returns Biggs 
<%= bar %> //returns Wedge

However, If you are NOT using the term partials: in your render, for example like so:

但是,如果您没有使用“偏置”一词,例如在您的渲染中:

<% render 'your_partial', locals: {foo: 'Biggs', bar: 'Wedge'} %>

<%渲染'your_partial', local: {foo: 'Biggs', bar: 'Wedge'} %>

Then your locals will be available as members of locals, accessible thus:

那么你的当地人就会成为当地人,因此,

<% locals[:foo] %> //returns Biggs
<% locals[:bar] %> //returns Wedge

#3


0  

It's working fine with me.. where is your _tabs.html.erb located at?

这对我来说没问题。在哪里你的_tabs.html。erb位于?

#4


0  

Try something like this:

试试这样:

<%= render partial: "form" , locals:{ name_you_want_use_in_partial: @varialble_to_pass } %>

I had the same problem. So I looked into a documentation Ruby on Rails - passing local... and I used fllowing notation - now it's working :)

我也有同样的问题。所以我查看了一个Ruby on Rails的文档—传递本地…我用了fllowing符号,现在它开始工作了

#1


50  

I've noticed that if I did not include 'partial:' before my partial path, like so:

我注意到,如果在我的部分路径之前没有包含“部分:”,就像这样:

<%= render 'my_partial', :locals => {:class_Name => "Science", :y => 36} %>

I was required to use the hash+symbol in my partial to access the desired values as others have noted.

我被要求在我的部分中使用散列+符号来访问所需的值。

<div>
<h1> <%= locals[:class_Name] %> has a y value of <%= locals[:y] %></h1>
</div>

However, including 'partial:' before my partial path:

但是,包括“部分:”在我的部分路径之前:

<% render partial: 'my_partial', :locals => {:class_Name => 'Science', :y => 36 } %>

...allowed me to simply call the hash values directly.

…允许我直接调用哈希值。

<div>
<h1><%= class_Name %> has a y value of <%= y %></h1>
</div>

Just something to keep in mind, this issue tripped me up for awhile when trying to send the locals hash to my partial.

需要记住的是,这个问题让我有一段时间都搞砸了。

#2


19  

If you are using the term partial: in your render, for example like so:

如果你使用的是局部:在你的渲染中,例如:

<% render partial: 'your_partial', locals: {foo: 'Biggs', bar: 'Wedge'} %>

<%渲染部分:'your_partial', local: {foo: 'Biggs', bar: 'Wedge'} %>。

Then your locals will be available simply as foo and bar, directly accessibly thus:

然后,你的本地用户将以foo和bar的形式提供,直接可访问如下:

<%= foo %> //returns Biggs 
<%= bar %> //returns Wedge

However, If you are NOT using the term partials: in your render, for example like so:

但是,如果您没有使用“偏置”一词,例如在您的渲染中:

<% render 'your_partial', locals: {foo: 'Biggs', bar: 'Wedge'} %>

<%渲染'your_partial', local: {foo: 'Biggs', bar: 'Wedge'} %>

Then your locals will be available as members of locals, accessible thus:

那么你的当地人就会成为当地人,因此,

<% locals[:foo] %> //returns Biggs
<% locals[:bar] %> //returns Wedge

#3


0  

It's working fine with me.. where is your _tabs.html.erb located at?

这对我来说没问题。在哪里你的_tabs.html。erb位于?

#4


0  

Try something like this:

试试这样:

<%= render partial: "form" , locals:{ name_you_want_use_in_partial: @varialble_to_pass } %>

I had the same problem. So I looked into a documentation Ruby on Rails - passing local... and I used fllowing notation - now it's working :)

我也有同样的问题。所以我查看了一个Ruby on Rails的文档—传递本地…我用了fllowing符号,现在它开始工作了