{"id":9415,"date":"2024-01-29T04:39:52","date_gmt":"2024-01-29T04:39:52","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=9415"},"modified":"2024-06-27T10:40:08","modified_gmt":"2024-06-27T10:40:08","slug":"ruby-on-rails-app-works-locally-but-does-not-work-on-heroku","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/ruby-on-rails\/ruby-on-rails-app-works-locally-but-does-not-work-on-heroku","title":{"rendered":"Ruby on Rails App Works Locally But Does Not Work On Heroku"},"content":{"rendered":"<h2>Solution 1<\/h2>\n<h3>Error Indication<\/h3>\n<p>The description of the problems says that there is no method defined as find_by_remember_token for the User Class.<\/p>\n<p>When a developer tries to run a method that is not defined on the object in Ruby, it typically raises a <strong>NoMethodError<\/strong>. This error occurs when you attempt to invoke a method on an object that doesn&#8217;t have that method defined.<\/p>\n<p>Please see the logs shared from the issue.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">\r\n2012-07-18T06:10:26+00:00 app[web.1]: ActionView::Template::Error (undefined method `find_by_remember_token' for #&lt;Class:0x00000004960740&gt;):\r\n<\/pre>\n<h3>Error Diagnosis <\/h3>\n<p>As per my thinking the questioner, Created added a new column remember_token to its user model. To add a new column to an attribute in Rails users need to create a new migration file. You can see the syntax of adding a new column to an model<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">\r\nclass NameOfYourMigration < ActiveRecord::Migration\r\n  def change\r\n    add_column :users, :remember_token, :string\r\n  end\r\nend\r\n<\/pre>\n<p>After creating a migration user needs to run the following command to reflect migration changes into the database changes.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">\r\nrake db:migrate\r\n<\/pre>\n<p>Once you successfully run the migration the new attribute is added to the table. <\/p>\n<p>Users can verify newly created columns added to respective tables by checking the schema.rb OR Users can open the rails console and check the model attributes.<br \/>\nThe find_by_remember_token is a type of Dynamic finder method. These methods are automatically created based on the database columns of a model.<\/p>\n<p>To deploy the same changes on the Heroku user needs to follow the same steps.<\/p>\n<p>Push the changes to Heroku application using the following command.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">\r\ngit push heroku master\r\n<\/pre>\n<p>After pushing the changes, the user needs to run the migrate command on the heroku as well. The command to run migration on the heroku listed below:-<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">\r\nheroku run rake db:migrate\r\n<\/pre>\n<h3>Error Cause<\/h3>\n<p>If a user experiences an <strong>undefined method 'find_by_remember_token'<\/strong> error on Heroku after running a migration locally, it's possible that the migration hasn't been applied on the Heroku database<\/p>\n<h3>Error Resolutions<\/h3>\n<p>After running the migration locally, make sure to apply the migration on your Heroku app. Use the following command:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">\r\nheroku run rake db:migrate\r\n<\/pre>\n<p>After applying the migration, restart your Heroku app to ensure that any changes take effect:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">\r\nheroku restart \u2013app application_name\r\n<\/pre>\n<p>You can also try running the problematic method in the Heroku Rails console to see if the issue persists:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">\r\nheroku run rails console\r\n<\/pre>\n<p>And then in the console:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">\r\nUser.find_by_remember_token(token)\r\n<\/pre>\n<h2>Solution 2<\/h2>\n<h3>Error Indication<\/h3>\n<p>The description of the problems says that there is no method defined as find_by_remember_token for the User Class.<br \/>\nWhen a developer tries to run a method that is not defined on the object in Ruby, it typically raises a <strong>NoMethodError<\/strong>. This error occurs when you attempt to invoke a method on an object that doesn't have that method defined.<\/p>\n<h3>Error Resolutions<\/h3>\n<p>Further checking the Hartl Tutorial I have identified the missing piece of information. We need to add the remember_token to the attr_accessible. You can add the attr_accessible using the following way.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">\r\nclass User < ActiveRecord\r\n  attr_accessible :name, :email, :password, :password_confirmation, :remember_token\r\nend\r\n<\/pre>\n<p>Adding <strong>:remember_token<\/strong> to the list of accessible attributes using attr_accessible is a common step in Ruby on Rails 3 applications, especially when dealing with user authentication and sessions.<\/p>\n<p>To further clarify, the attr_accessible method is a part of the protected_attributes gem in Rails. It allows you to specify which attributes can be mass-assigned using methods like update_attributes or during the creation of a new record.<\/p>\n<p>The addition of :remember_token to the accessible attributes ensures that the remember_token attribute can be properly assigned when needed, which is crucial for functionality related to user sessions, authentication, and remember me functionality.<\/p>\n<p>After adding <strong>:remember_token<\/strong> to the list existing att_accesssible . User needs to push the changes to the heroku using the following command.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">\r\ngit push heroku master\r\n<\/pre>\n<p>After Pushing the changes User needs to restart the heroku application.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">\r\nheroku restart \u2013app application_name\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Solution 1 Error Indication The description of the problems says that there is no method defined as find_by_remember_token for the User Class. When a developer tries to run a method that is not defined on the object in Ruby, it typically raises a NoMethodError. This error occurs when you attempt to invoke a method on [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":9416,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[11],"tags":[],"class_list":["post-9415","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ruby-on-rails"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/9415"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=9415"}],"version-history":[{"count":3,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/9415\/revisions"}],"predecessor-version":[{"id":9419,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/9415\/revisions\/9419"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/9416"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=9415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=9415"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=9415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}