Rails 3:“未定义的局部变量或方法"如果我把内容放在部分 [英] Rails 3: "undefined local variable or method" if I put content in a partial

查看:38
本文介绍了Rails 3:“未定义的局部变量或方法"如果我把内容放在部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下表

<头><tr><th colspan="2">照片评级</th></tr><tr><td>平均评分</td><td><%=msg.average_rating %></td></tr><tr><td>您的评分</td><td><%= current_user_rating %></td></tr></thead>

如果我把它放在一个视图中就可以了,但是当我把它放在部分 _rating.html.erb 中时,我得到

未定义的局部变量或方法 'msg' for #<#:0x003463d9f388>

我通过 <%= render :partial => 链接到部分"rating" %>,我必须在部分链接中添加什么才能避免出现错误?谢谢

解决方案

您需要将 msg 变量添加到局部模板的局部变量中.

<%= render :partial =>"评级", :locals =>{ :msg =>味精 } %>

I have the following table

<table id="rating">
    <thead>
        <tr>
            <th colspan="2">Photo Ratings</th>
        </tr>
    <tr>
        <td>Average Rating</td>
        <td><%= msg.average_rating %></td>
    </tr>
    <tr>
        <td>Your Rating</td>
        <td><%= current_user_rating %></td>
    </tr>
    </thead>
</table>

Which works fine if I have it in a view, but when I put it in a partial _rating.html.erb I get

undefined local variable or method 'msg' for #<#<Class:0x000003463da1570>:0x003463d9f388>

I am linking to the partial via <%= render :partial => "rating" %>, what must I add to the partial link so that I dont get the error? Thanks

解决方案

You need to add the msg variable to the locals of the partial template.

<%= render :partial => "rating", :locals => { :msg => msg } %>

这篇关于Rails 3:“未定义的局部变量或方法"如果我把内容放在部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆