ruby-on-rails - Heroku 错误:ActionView::Template::Error(#<Message:0x007fc9df016930> 的未定义方法 `captcha')

标签 ruby-on-rails ruby postgresql heroku

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

最佳答案

也许您在第一次运行 heroku run rake db:migrate 并包含该迁移后添加了 captcha 列?如果是这种情况,您需要重置数据库(请注意,这将清除所有数据):

heroku pg:reset DATABASE

然后再次迁移

heroku run rake db:migrate

之后验证码也应该可以在 Heroku 上使用。

关于ruby-on-rails - Heroku 错误:ActionView::Template::Error(#<Message:0x007fc9df016930> 的未定义方法 `captcha'),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32158960/

相关文章:

ruby-on-rails - 运行 rails server 时克隆的项目不会启动

ruby - 我如何安装 Command-T、Pathogen 而不使用 RVM?

postgresql - 从 CSV 填充 postgres 表列时出现非空约束错误

postgresql - 如何在 PostgreSQL 中设置独占锁的超时时间

Django:如何从 ManyToMany 迁移到 ForeignKey?

ruby-on-rails - Docker on Apple Silicon (M1) 中的 Ruby on Rails

mysql - 如何使用 Active Record 查询接口(interface)将 MySQL 数据库 View 转换为 Rails

python - 如何在 python 中删除具有给定标志的字典中的条目?

ruby - 使用切片!在变量上修改填充变量的节点属性

ruby-on-rails - 跳过 after_commit on destroy in rails