view无法识别rails中控制器的变量

时间:2016-06-20 06:44:04

标签: ruby-on-rails

大家好!我在rails中遇到了问题。 我有一些像这样的代码:

class UsersController < ApplicationController
def index
@users = User.all
end

<% @users.each do |user| %>
      <tbody>
            <tr>
                <td><%=stt%></td>
                <td> <%= link_to user.name, user %></td>
                <td><%=user.first_name + ' ' + user.last_name%></td>
                <td><%=user.phone%></td>
          <td><%=user.email%></td>
            </tr>
      </tbody> 

在本地可以,但是当我在主机上部署它时,会出现错误:

Started GET "/users" for 180.93.29.249 at 2016-06-20 06:34:34 +0000
App 7027 stdout: Processing by UsersController#index as HTML
App 7027 stdout:   ^[[1m^[[36mUser Load (0.2ms)^[[0m  ^[[1mSELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1^[[0m  [["id", 1]]
App 7027 stdout:   ^[[1m^[[35mCACHE (0.0ms)^[[0m  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
App 7027 stdout:   ^[[1m^[[36mCACHE (0.0ms)^[[0m  ^[[1mSELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1^[[0m  [["id", 1]]
App 7027 stdout:   ^[[1m^[[35mUser Load (0.5ms)^[[0m  SELECT "users".* FROM "users"
App 7027 stdout:   Rendered users/index.html.erb within layouts/application (80.6ms)
App 7027 stdout: Completed 500 Internal Server Error in 241ms (ActiveRecord: 0.7ms)
App 7027 stdout:
App 7027 stdout: ActionView::Template::Error (undefined method `+' for nil:NilClass):
App 7027 stdout:     31:                        <tr>
App 7027 stdout:     32:                                <td><%=stt%></td>
App 7027 stdout:     33:                                <td> <%= link_to user.name, user %></td>
App 7027 stdout:     34:                                <td><%=user.first_name + ' ' + user.last_name%></td>
App 7027 stdout:     35:                                <td><%=user.phone%></td>
App 7027 stdout:     36:               <td><%=user.email%></td>
App 7027 stdout:     37:               <%stt+=1%>
App 7027 stdout:   app/views/users/index.html.erb:34:in `block in _app_views_users_index_html_erb___2721014350791878178_25989480'
App 7027 stdout:   app/views/users/index.html.erb:29:in `_app_views_users_index_html_erb___2721014350791878178_25989480'

请帮助!!!

1 个答案:

答案 0 :(得分:0)

您可以将以下内容重写为:

<% @users.each do |user| %>
  <tbody>
        <tr>
            <td><%=stt%></td>
            <td> <%= link_to user.name, user %></td>
            <td><%= "#{user.first_name} #{user.last_name}" %></td>
            <td><%= user.phone %></td>
      <td><%=user.email%></td>
        </tr>
  </tbody>