4

我在 Rails 应用程序中设置用户路由时遇到问题。

在 routes.rb 我添加了resources :users.

TestApp::Application.routes.draw do
     resources :users

  root :to => 'xxx#home' 
  match '/about', to: 'xxx#about'
  match '/test', to: 'xxx#test'
  match '/news', to: 'xxx#news'
  match '/signup', to: 'users#new'

end

在 users_controller.rb 我添加了:

class UsersController < ApplicationController
  def new
  end

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

我创建了带有以下行的新 show.html.rb 文件:

<%= @user.name %>, <%= @user.email %>

但是当我部署到 heroku 时,我得到了这个信息,但我看不到那个页面。

heroku[router]: at=info method=GET path=/users/1

有什么解决办法吗,还是我做错了什么?

4

2 回答 2

1

您确定id=1heroku 上存在的用户吗?如果有,你用过吗?

$ heroku run console

此命令在生产环境中打开 rails 控制台。在里面,你做过类似的事情吗?

u = User.create(somehash_with_attrs)
# .. or
u = User.new
# ... 
u.save
u.id # => 1 or 23
# If your are not sure which id your user has try the following
User.first.id

您是否知道这yourapp.domain/users/1指向用户记录,id=1 而不是数据库中的第一条记录。

于 2012-12-03T07:05:17.650 回答
0

让我们看看您的迁移文件。PG 找不到名为 Users 的表(强调复数)。因此,在您运行迁移之后,它看起来像是找到了表。

您是如何在控制台中创建用户的?如果你使用过,比如 u = User.new 或 u = User.build,请确保执行 u.save 以确保它实际保存到数据库中。

于 2012-12-03T05:12:42.647 回答