在我的本地环境中,一切正常。当我尝试部署到heroku并最初查看我的网站时,它给了我以下错误:"我们很抱歉,但出了点问题。如果您是应用程序所有者,请检查日志以获取更多信息。"
当我查看" heroku日志"时,我发现此错误消息: ActionView :: Template :: Error(未定义方法`验证码' for -Message:0x007fc9df016930 - )
HTML表单视图/ 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 %>
页面控制器
class PagesController < ApplicationController
def index
@message = Message.new
@a = rand(9)
@b = rand(9)
session["sum"] = @a + @b
end
end
消息模型
class Message < ActiveRecord::Base
validates :email, :user_message, presence: true
end
消息控制器
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
消息迁移
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
模式
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
路线
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
网站
http://ChrisPelnar.com
答案 0 :(得分:3)
也许您在首次运行heroku run rake db:migrate
之后添加了验证码列,其中包含该迁移?如果是这种情况,您需要重置数据库(请注意,这将清除所有数据)
heroku pg:reset DATABASE
然后再次使用
进行迁移heroku run rake db:migrate
此后,验证码也应该在Heroku上可用。
答案 1 :(得分:0)
尝试一下,
heroku run rake db:migrate
heroku restart
OR
如果上述操作因某种原因失败,您也可以尝试重置数据库。
rake db:drop db:create db:reset
或
rake db:drop:_unsafe db:create db:reset
然后,再次按下Heroku(下面的示例代码)
git push origin master
git push heroku master
我有类似的错误,对我有用。