ruby on rails - App works fine on Localhost, but has NoMethodError (undefined method `-' for nil:NilClass) on Heroku -
i new ruby , experiencing error after deploying app heroku.
i test app on localhost , works fine; however, when try save new post, have "undefined method `-' nil:nilclass" on heroku. have searched solutions on stackoverflow far haven't found anything.
error got heroku logs:
2015-04-01t03:37:23.412066+00:00 app[web.1]: nomethoderror (undefined method `-' nil:nilclass): 2015-04-01t03:37:23.412076+00:00 app[web.1]: app/controllers/topics/posts_controller.rb:22:in `create' 2015-04-01t03:37:23.412074+00:00 app[web.1]: app/models/post.rb:37:in `save_with_initial_vote' 2015-04-01t03:37:23.407819+00:00 app[web.1]: (1.0ms) rollback 2015-04-01t03:37:23.412070+00:00 app[web.1]: app/models/vote.rb:20:in `update_post' 2015-04-01t03:37:23.412078+00:00 app[web.1]: 2015-04-01t03:37:23.412063+00:00 app[web.1]: 2015-04-01t03:37:23.412068+00:00 app[web.1]: app/models/post.rb:30:in `update_rank' 2015-04-01t03:37:23.412072+00:00 app[web.1]: app/models/post.rb:39:in `block in save_with_initial_vote'
from logs, looks problem update_rank method in post.rb.
from app/models/post.rb:
class post < activerecord::base has_many :comments, dependent: :destroy has_many :votes, dependent: :destroy has_many :favorites, dependent: :destroy belongs_to :user belongs_to :topic default_scope { order('rank desc') } scope :visible_to, ->(user) { user ? : joins(:topic).where('topics.public' => true) } validates :title, length: { minimum: 5 }, presence: true validates :body, length: { minimum: 20 }, presence: true validates :topic, presence: true validates :user, presence: true def update_rank age_in_days = (created_at - time.new(1970,1,1)) / (60 * 60 * 24) new_rank = points + age_in_days update_attribute(:rank, new_rank) end def save_with_initial_vote activerecord::base.transaction post = post.create user.votes.create(value: 1, post: self) end end
from app/controllers/topics/posts_controller.rb:
def create @topic = topic.find(params[:topic_id]) @post = current_user.posts.build(post_params) @post.topic = @topic authorize @post if @post.save_with_initial_vote flash[:notice] = "post saved." redirect_to [@topic, @post] else flash[:error] = "there error saving post. please try again." render :new end end
models/vote.rb:
after_save :update_post def up_vote? value == 1 end def down_vote? value == -1 end def update_post post.update_rank end
i have run "heroku rn db:migrate" , "heroku restart" error still exists. rails version 4.2.0 , ruby version 2.0.0.
does know cause , how make work on heroku?
the use of "-" in code provided in first line of post#update_rank
note, when run following in irb
irb> (nil - time.new(1970,1,1)) / (60 * 60 * 24)
i following error.
nomethoderror: undefined method `-' nil:nilclass
remember in ruby class. many classes implement operator methods. datetime class does, nilclass not.
i suspect created_at not yet set , @ point nil value.
Comments
Post a Comment