接收语法错误,意外'=',期望关键字_when代码

时间:2012-02-29 17:02:20

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1

我有一个表与用户的has_many关联的案例。我试图通过在用户显示页面中呈现它来显示与用户相关的所有案例。我无法在这里渲染案例。任何帮助,将不胜感激。

我的用户显示页面代码:

 <table >
  <tr>
    <td >
      <h1>
        Welcome <%= current_user.first_name %>  you are <%= current_user.role %>    
      </h1>
    </td>
    </tr>
    <tr>
      <td>
        <% render @cases %>
      </td>
    </tr>
    </table>

在用户中显示控制器:

     def show
    @user = User.find(params[:id])
    @cases = @user.cases
    @title = @user.first_name
    end

我在views / cases中有一个文件_case.html.erb。

1 个答案:

答案 0 :(得分:6)

问题是case是Ruby中的保留字,并且在render @cases时,它试图在partial中创建一个名为case的局部变量,这是不允许的。你必须调用local变量。 docs here解释了这个例子:

# Renders a collection of partials but with a custom local variable name
render :partial => "admin_person", :collection => @winners, :as => :person

在你的情况下:

render :partial => 'case', :collection => @cases, :as => :current_case

然后在部分情况下,只需使用变量current_case,否则您将使用变量case