创作在本地工作,但不在 Heroku 上工作.ActiveRecord::UnknownAttributeError (



用户只有在登录时才能创建指南。

当我点击"新指南"链接时,这就是Heroku的日志:

2013-12-30T20:28:37.826032+00:00 app[web.1]: ActiveRecord::UnknownAttributeError (unknown attribute: user_id):

指南控制器:

class GuidesController < ApplicationController
  before_action :set_guide, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]
  # GET /guides
  # GET /guides.json
  def index
    if params[:tag]
      @guides = Guide.tagged_with(params[:tag])
    else
      @guides = Guide.all
    end
  end
  # GET /guides/1
  # GET /guides/1.json
  def show
  end
  # GET /guides/new
  def new
    @guide = current_user.guides.build(guide_params)
  end
  # GET /guides/1/edit
  def edit
  end
  # POST /guides
  # POST /guides.json
  def create
    @guide = current_user.guides.build(guide_params)
    respond_to do |format|
      if @guide.save
        format.html { redirect_to @guide, notice: 'Guide was successfully created.' }
        format.json { render action: 'show', status: :created, location: @guide }
      else
        format.html { render action: 'new' }
        format.json { render json: @guide.errors, status: :unprocessable_entity }
      end
    end
  end
  # PATCH/PUT /guides/1
  # PATCH/PUT /guides/1.json
  def update
    respond_to do |format|
      if @guide.update(guide_params)
        format.html { redirect_to @guide, notice: 'Guide was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @guide.errors, status: :unprocessable_entity }
      end
    end
  end
  # DELETE /guides/1
  # DELETE /guides/1.json
  def destroy
    @guide.destroy
    respond_to do |format|
      format.html { redirect_to guides_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_guide
      @guide = Guide.find(params[:id])
    end
    # Never trust parameters from the scary internet, only allow the white list through.
    def guide_params
      params.require(:guide).permit(:title, :author, :description, :link, :tag_list) if params[:guide]
    end
end

你在new操作中有这个

def new
  @guide = current_user.guides.build(guide_params)
end

为什么?新操作应仅将表单返回到浏览器以创建新指南。你在create操作中重复此操作,它应该在的位置。

你的index也有这个:

def index
  if params[:tag]
    @guides = Guide.tagged_with(params[:tag])
  else
    @guides = Guide.all
  end
end

您可能应该使用guide_params[:tag]因为浏览器返回:tag

编辑我看到您在白名单中使用[:tag_list]。 我猜你是把那个交给别的地方吧?您是否测试过在定义标记的情况下执行index操作的能力?我认为您唯一想使用裸params[:xxxx]的地方是在私有方法中。

最新更新