添加新表单字段后,我得到未定义的方法rails

时间:2014-01-18 12:53:37

标签: ruby-on-rails ruby database

在我的rails数据库中添加一个新列后,我尝试将其添加到我的表单中,最后以

结束
<div class="field">
    <%= f.label :preview %><br>
    <%= f.text_field :preview %>
</div>

当我导航到视图时,我得到未定义的方法preview控制台输出:

E:\RailsCode\Website>rails generate migration ImprovePages pageNumber:integer preview:string
      invoke  active_record
      create    db/migrate/20140118121716_improve_pages.rb

E:\RailsCode\Website>rake db:migrate
==  ImprovePages: migrating ===================================================
==  ImprovePages: migrated (0.0000s) ==========================================

此外,在db控制台中,它声明列不存在。模型是空白的(只是类的定义)和控制器

class PagesController < ApplicationController
  before_action :set_page, only: [:show, :edit, :update, :destroy]

  # GET /pages
  # GET /pages.json
  def index
    @pages = Page.all
  end

  # GET /pages/1
  # GET /pages/1.json
  def show
  end

  # GET /pages/new
  def new
    @page = Page.new
  end

  # GET /pages/1/edit
  def edit
  end

  # POST /pages
  # POST /pages.json
  def create
    @page = Page.new(page_params)

    respond_to do |format|
      if @page.save
        format.html { redirect_to @page, notice: 'Page was successfully created.' }
        format.json { render action: 'show', status: :created, location: @page }
      else
        format.html { render action: 'new' }
        format.json { render json: @page.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /pages/1
  # PATCH/PUT /pages/1.json
  def update
    respond_to do |format|
      if @page.update(page_params)
        format.html { redirect_to @page, notice: 'Page was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @page.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /pages/1
  # DELETE /pages/1.json
  def destroy
    @page.destroy
    respond_to do |format|
      format.html { redirect_to pages_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_page
      @page = Page.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def page_params
      params.require(:page).permit(:thumbnail, :img, :title, :body, :preview, :pageNumber, :preview)
    end
end

编辑:迁移确实为空,重新运行genreate迁移继续产生空迁移

1 个答案:

答案 0 :(得分:1)

class AddPreviewColumn < ActiveRecord::Migration
  def change
    add_column :pages, :preview, :string
  end
end

语法:

  

add_column:table_name,:attribute,:type