【问题标题】:Heroku giving error on setting a field on a model for its view in RailsHeroku 在为其在 Rails 中的视图设置模型字段时出错
【发布时间】:2012-11-16 20:45:56
【问题描述】:

我非常简单地在 UsersController#new 中的用户模型上设置一个字段:

def new
  @user = User.new
  @user.next_billing_cycle = Date.today

  respond_to do |format|
    format.html # new.html.erb
    format.json { render json: @user }
  end
end

在我的开发环境中完美运行。我的用户模型有attr_accessible :next_billing_cycle,所以它也有相应的date_select 字段集:

class User < ActiveRecord::Base
  attr_accessible :name, :next_billing_cycle
end

但是,在 Heroku 中,我收到此错误:

2012-11-28T21:02:47+00:00 app[web.1]: Started GET "/users/new" for 173.55.174.235 at 2012-11-28 21:02:47 +0000
2012-11-28T21:02:47+00:00 heroku[router]: at=info method=GET path=/users/new host=example.com fwd=173.55.174.235 dyno=web.1 queue=0 wait=0ms connect=1ms service=402ms status=500 bytes=643
2012-11-28T21:02:47+00:00 app[web.1]: 
2012-11-28T21:02:47+00:00 app[web.1]: NoMethodError (undefined method `next_billing_cycle=' for #<User:0x00000004f47618>):
2012-11-28T21:02:47+00:00 app[web.1]: 
2012-11-28T21:02:47+00:00 app[web.1]: Completed 500 Internal Server Error in 322ms
2012-11-28T21:02:47+00:00 app[web.1]: 
2012-11-28T21:02:47+00:00 app[web.1]:   app/controllers/users_controller.rb:29:in `new'
2012-11-28T21:02:47+00:00 app[web.1]: Processing by UsersController#new as HTML

更新:我应该说清楚,我确实运行了heroku rake db:migrate

【问题讨论】:

  • 您是否在远程数据库上运行了所有迁移?
  • 如果您在 Heroku 启动应用后运行迁移,您可能需要 heroku restart,因为 Rails 在生产环境中默认缓存数据库模式。
  • @willglynn - 我没有做heroku restart。我目前不能,但会在不久的将来尝试。
  • 我只是在回顾我的未解决问题,我遇到了这个问题......我解决了它,但根本不记得我是怎么做到的 :(,对不起。我猜heroku restart 修复了它,因为我不记得在那之后做了任何事情。

标签: ruby-on-rails ruby-on-rails-3 activerecord heroku ruby-on-rails-3.2


【解决方案1】:

您的意思是使用attr_accessor 吗?还是next_billing_cycle 是数据库中的一列?

【讨论】:

  • 如果它在开发环境中有效,但在 Heroku 上无效,则可能是您遇到了迁移问题。您确定 Heroku 上存在该列吗?
  • 他使用的是attr_accessible,而不是attr_accessor
  • @willglynn 对。如果它是 attr_accessor 那么 next_billing_cycle= 将被定义。 attr_accessible 只是一个白名单,我相信你知道。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-19
  • 1970-01-01
  • 1970-01-01
  • 2012-10-25
  • 2019-04-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多