Heroku错误方法=GET路径



我正试图使用RubyonRails构建一个Pinteresting克隆。

这一切都可以离线工作,但当我在heroku上点击这里的"新Pin"时,我会收到这个错误https://fathomless-hamlet-4281.herokuapp.com/pins

我已经运行了heroku run rake:db migrate

当我运行heroku日志时——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

这是我的pin控制器:

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

我想,请在迁移后重新启动服务器将刷新架构缓存。

heroku重启

如果你找不到解决方案您将得到UnknownAttributeError,因为您的pin表中没有名为user_id 的列

user_id不存在ActiveRecord::UnknownAttribute错误表示表引脚中缺少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:migrategit 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

最新更新