Heroku 错误方法=GET 路径
Heroku Error method=GET path
我正在尝试在 Rails 上使用 Ruby 构建一个 Pinteresting 克隆。
它都可以离线工作,但是当我在 heroku 上点击 "New Pin" 时出现这个错误 https://fathomless-hamlet-4281.herokuapp.com/pins
我有 运行 heroku 运行 rake:db 迁移
当我 运行 heroku logs --tail 这是我点击按钮时出现的内容。
有人有线索吗?非常感谢。
←[36m2015-02-19T10:29:05.656883+00:00 app[web.1]:←[0m
←[36m2015-02-19T10:29:05.656885+00:00 app[web.1]:←[0m
←[33m2015-02-19T10:29:05.953458+00:00 heroku[router]:←[0m at=info method=GET path="/pins/new" host=fathomless-hamlet-4281.herokuapp.com request_id=5259a5f0-869a-41cb-b360-a27c8b59dd0d fwd="101.167.115.4" dyno=web.1 connect=3ms service=10ms status=500 bytes=1754
←[36m2015-02-19T10:29:05.946125+00:00 app[web.1]:←[0m Started GET "/pins/new" for 101.167.115.4 at 2015-02-19 10:29:05 +0000
←[36m2015-02-19T10:29:05.951561+00:00 app[web.1]:←[0m Completed 500 Internal Server Error in 3ms
←[36m2015-02-19T10:29:05.948081+00:00 app[web.1]:←[0m Processing by PinsController#new as HTML
←[36m2015-02-19T10:29:05.952642+00:00 app[web.1]:←[0m
←[36m2015-02-19T10:29:05.952645+00:00 app[web.1]:←[0m ActiveRecord::UnknownAttributeError (unknown attribute: user_id):
←[36m2015-02-19T10:29:05.952647+00:00 app[web.1]:←[0m app/controllers/pins_controller.rb:14:in `new'
←[36m2015-02-19T10:29:05.952648+00:00 app[web.1]:←[0m
←[36m2015-02-19T10:29:05.952650+00:00 app[web.1]:←[0m
这是我的引脚控制器:
class PinsController < ApplicationController
before_action :set_pin, only: [:show, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def index
@pins = Pin.all
end
def show
end
def new
@pin = current_user.pins.build
end
def edit
end
def create
@pin = current_user.pins.build(pin_params)
if @pin.save
redirect_to @pin, notice: 'Pin was successfully created.'
else
render action: 'new'
end
end
def update
if @pin.update(pin_params)
redirect_to @pin, notice: 'Pin was successfully updated.'
else
render action: 'edit'
end
end
def destroy
@pin.destroy
redirect_to pins_url
end
private
# Use callbacks to share common setup or constraints between actions.
def set_pin
@pin = Pin.find(params[:id])
end
def correct_user
@pin = current_user.pins.find_by(id: params[:id])
redirect_to pins_path, notice: "Not authorized to edit this pin" if @pin.nil?
end
# Never trust parameters from the scary internet, only allow the white list through.
def pin_params
params.require(:pin).permit(:description, :image)
end
end
这是我的观点>图钉>index.html.erb
<h1>Listing pins</h1>
<table>
<thead>
<tr>
<th>Image</th>
<th>Description</th>
<th>User</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @pins.each do |pin| %>
<tr>
<td><%= image_tag pin.image.url (:medium) %></td>
<td><%= pin.description %></td>
<td><%= pin.user.email if pin.user %></td>
<td><%= link_to 'Show', pin %></td>
<% if pin.user == current_user %>
<td><%= link_to 'Edit', edit_pin_path(pin) %></td>
<td><%= link_to 'Destroy', pin, method: :delete, data: { confirm: 'Are you sure?' } %>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
<br>
<% if user_signed_in? %>
<%= link_to 'New Pin', new_pin_path %>
<% end %>
<%= link_to 'New Pin', new_pin_path %>
下面是Schema.rb
ActiveRecord::Schema.define(version: 20150210113737) do
create_table "pins", force: true do |t|
t.string "description"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
end
add_index "pins", ["user_id"], name: "index_pins_on_user_id"
create_table "users", force: true 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.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
下面是20150205130111_add_user_id_to_pins
class AddUserIdToPins < ActiveRecord::Migration
def change
add_column :pins, :user_id, :interger
add_index :pins, :user_id
end
end
我猜,迁移后重新启动服务器会刷新架构缓存。
如果你没有找到解决方案
您收到 UnknownAttributeError 是因为您的 Pin table 中没有名为 user_id
的列
user_id
不存在 ActiveRecord::UnknownAttribute
错误意味着您在 table 引脚中缺少 user_id
。
创建迁移
bundle exec rails generate migration AddUserIdToPins user_id:integer
然后
bundle exec rake db:migrate
然后
heroku restart
如果您已经提交了文件,请先删除该文件并提交给 heroku
del c:/Users/Joey/desktop/pinteresting/db/migrat e/20150205130111_add_user_id_to_pins.rb
,
然后做
bundle exec rails generate migration AddUserIdToPins user_id:integer
然后是
bundle exec rake db:migrate
和
git rm old_migration_file
和
git add new_migration_file
和
git push heroku master
heroku run rake db:migrate
在您的控制器代码中也请更改
# Never trust parameters from the scary internet, only allow the white list through.
def pin_params
params.require(:pin).permit(:description, :image, :user_id)
end
我正在尝试在 Rails 上使用 Ruby 构建一个 Pinteresting 克隆。
它都可以离线工作,但是当我在 heroku 上点击 "New Pin" 时出现这个错误 https://fathomless-hamlet-4281.herokuapp.com/pins
我有 运行 heroku 运行 rake:db 迁移
当我 运行 heroku logs --tail 这是我点击按钮时出现的内容。
有人有线索吗?非常感谢。
←[36m2015-02-19T10:29:05.656883+00:00 app[web.1]:←[0m
←[36m2015-02-19T10:29:05.656885+00:00 app[web.1]:←[0m
←[33m2015-02-19T10:29:05.953458+00:00 heroku[router]:←[0m at=info method=GET path="/pins/new" host=fathomless-hamlet-4281.herokuapp.com request_id=5259a5f0-869a-41cb-b360-a27c8b59dd0d fwd="101.167.115.4" dyno=web.1 connect=3ms service=10ms status=500 bytes=1754
←[36m2015-02-19T10:29:05.946125+00:00 app[web.1]:←[0m Started GET "/pins/new" for 101.167.115.4 at 2015-02-19 10:29:05 +0000
←[36m2015-02-19T10:29:05.951561+00:00 app[web.1]:←[0m Completed 500 Internal Server Error in 3ms
←[36m2015-02-19T10:29:05.948081+00:00 app[web.1]:←[0m Processing by PinsController#new as HTML
←[36m2015-02-19T10:29:05.952642+00:00 app[web.1]:←[0m
←[36m2015-02-19T10:29:05.952645+00:00 app[web.1]:←[0m ActiveRecord::UnknownAttributeError (unknown attribute: user_id):
←[36m2015-02-19T10:29:05.952647+00:00 app[web.1]:←[0m app/controllers/pins_controller.rb:14:in `new'
←[36m2015-02-19T10:29:05.952648+00:00 app[web.1]:←[0m
←[36m2015-02-19T10:29:05.952650+00:00 app[web.1]:←[0m
这是我的引脚控制器:
class PinsController < ApplicationController
before_action :set_pin, only: [:show, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def index
@pins = Pin.all
end
def show
end
def new
@pin = current_user.pins.build
end
def edit
end
def create
@pin = current_user.pins.build(pin_params)
if @pin.save
redirect_to @pin, notice: 'Pin was successfully created.'
else
render action: 'new'
end
end
def update
if @pin.update(pin_params)
redirect_to @pin, notice: 'Pin was successfully updated.'
else
render action: 'edit'
end
end
def destroy
@pin.destroy
redirect_to pins_url
end
private
# Use callbacks to share common setup or constraints between actions.
def set_pin
@pin = Pin.find(params[:id])
end
def correct_user
@pin = current_user.pins.find_by(id: params[:id])
redirect_to pins_path, notice: "Not authorized to edit this pin" if @pin.nil?
end
# Never trust parameters from the scary internet, only allow the white list through.
def pin_params
params.require(:pin).permit(:description, :image)
end
end
这是我的观点>图钉>index.html.erb
<h1>Listing pins</h1>
<table>
<thead>
<tr>
<th>Image</th>
<th>Description</th>
<th>User</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @pins.each do |pin| %>
<tr>
<td><%= image_tag pin.image.url (:medium) %></td>
<td><%= pin.description %></td>
<td><%= pin.user.email if pin.user %></td>
<td><%= link_to 'Show', pin %></td>
<% if pin.user == current_user %>
<td><%= link_to 'Edit', edit_pin_path(pin) %></td>
<td><%= link_to 'Destroy', pin, method: :delete, data: { confirm: 'Are you sure?' } %>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
<br>
<% if user_signed_in? %>
<%= link_to 'New Pin', new_pin_path %>
<% end %>
<%= link_to 'New Pin', new_pin_path %>
下面是Schema.rb
ActiveRecord::Schema.define(version: 20150210113737) do
create_table "pins", force: true do |t|
t.string "description"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
end
add_index "pins", ["user_id"], name: "index_pins_on_user_id"
create_table "users", force: true 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.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
下面是20150205130111_add_user_id_to_pins
class AddUserIdToPins < ActiveRecord::Migration
def change
add_column :pins, :user_id, :interger
add_index :pins, :user_id
end
end
我猜,迁移后重新启动服务器会刷新架构缓存。
如果你没有找到解决方案 您收到 UnknownAttributeError 是因为您的 Pin table 中没有名为 user_id
的列user_id
不存在 ActiveRecord::UnknownAttribute
错误意味着您在 table 引脚中缺少 user_id
。
创建迁移
bundle exec rails generate migration AddUserIdToPins user_id:integer
然后
bundle exec rake db:migrate
然后
heroku restart
如果您已经提交了文件,请先删除该文件并提交给 heroku
del c:/Users/Joey/desktop/pinteresting/db/migrat e/20150205130111_add_user_id_to_pins.rb
,
然后做
bundle exec rails generate migration AddUserIdToPins user_id:integer
然后是
bundle exec rake db:migrate
和
git rm old_migration_file
和
git add new_migration_file
和
git push heroku master
heroku run rake db:migrate
在您的控制器代码中也请更改
# Never trust parameters from the scary internet, only allow the white list through.
def pin_params
params.require(:pin).permit(:description, :image, :user_id)
end