简体   繁体   中英

Heroku Error: ActionView::Template::Error (undefined method `captcha' for #<Message:0x007fc9df016930>)

Everything is working correctly in my local environment. When I try to deploy to heroku and view my site initially it gives me the following error: "We're sorry, but something went wrong. If you are the application owner check the logs for more information."

When I check my "heroku logs", I find this error message: ActionView::Template::Error (undefined method `captcha' for -Message:0x007fc9df016930-)

HTML form views/pages/index.html.erb

  <%= form_for(@message) do |f| %>
  <%= f.text_field :first_name, :class => "message_name_input message_input_default", :placeholder => " First Name" %>
  <br><br>
  <%= f.text_field :last_name, :class => "message_name_input message_input_default", :placeholder => " Last Name" %>
  <br><br>
  <%= f.text_field :email, :required => true, :class => "message_email_input message_input_default", :placeholder => " * Email" %>
  <br><br>
  <%= f.text_area :user_message, :required => true, :class => "message_user-message_input", :placeholder => " * Write a message" %><br><br>

  <%= f.text_field :captcha, :required => true, :class => "message_input_default", :placeholder => " * #{@a} + #{@b} = ?" %><br><br>

  <div id="RecaptchaField2"></div>

  <%= f.submit "Send", :class => "messages_submit_button" %>

  <% end %>

Pages Controller

class PagesController < ApplicationController

  def index
    @message = Message.new

     @a = rand(9)

     @b = rand(9)
   session["sum"] = @a + @b
  end

end

Message Model

class Message < ActiveRecord::Base
  validates :email, :user_message, presence: true
end

Messages Controller

class MessagesController < ApplicationController


  def show
  end

  def new
    @message = Message.new
  end

  def create
  @message = Message.new(message_params)
    if params[:message][:captcha].to_i == session["sum"] && @message.save
      UserMailer.welcome_email(@message).deliver_now
      redirect_to '/message_sent'
    else
      redirect_to '/'
    end
  end

    private
  def message_params
    return params.require(:message).permit(:first_name, :last_name, :email, :user_message, :captcha)
  end


end

Messages Migration

class CreateMessages < ActiveRecord::Migration
  def change
    create_table :messages do |t|
      t.string :first_name
      t.string :last_name
      t.string :email
      t.string :user_message
      t.string :captcha
      t.timestamps null: false
    end
  end
end

Schema

ActiveRecord::Schema.define(version: 20150712164426) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "admins", force: :cascade do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.inet     "current_sign_in_ip"
    t.inet     "last_sign_in_ip"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "admins", ["email"], name: "index_admins_on_email", unique: true, using: :btree
  add_index "admins", ["reset_password_token"], name: "index_admins_on_reset_password_token", unique: true, using: :btree

  create_table "messages", force: :cascade do |t|
    t.string   "first_name"
    t.string   "last_name"
    t.string   "email"
    t.string   "user_message"
    t.string   "captcha"
    t.datetime "created_at",   null: false
    t.datetime "updated_at",   null: false
  end

end

Routes

Rails.application.routes.draw do
  devise_for :admins
  resources :pages
  resources :messages
  resources :admins

  get '/' => 'pages#index'
  get '/new' => 'messages#new'
  post '/message_sent' => 'messages#create'
  get '/message_sent' => 'messages#show'
end

WebSite

http://ChrisPelnar.com

Maybe you added the captcha column after originally running heroku run rake db:migrate the first time with that migration included? If that is the case, you need to reset the database ( Please note that this will clear all the data ) with

heroku pg:reset DATABASE

and then migrate it again with

heroku run rake db:migrate

After these the captcha should be available on Heroku too.

Try this,

heroku run rake db:migrate
heroku restart

OR,

You can also try by resetting your db, if above fails somehow.

rake db:drop db:create db:reset

or,

rake db:drop:_unsafe db:create db:reset

Then, push to the Heroku again (sample code below),

git push origin master
git push heroku master

I had similar error and it worked for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM