Rails 3: “undefined local variable or method” if I put content in a partial

霸气de小男生 提交于 2020-01-06 04:33:17

问题


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


回答1:


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

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


来源:https://stackoverflow.com/questions/7128123/rails-3-undefined-local-variable-or-method-if-i-put-content-in-a-partial

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!