After adding a new column to my rails databased I tried to add it to my form and ended up with
<div class="field">
<%= f.label :preview %><br>
<%= f.text_field :preview %>
</div>
When I navigate to the view I get undefined method preview
Console output:
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) ==========================================
Also, in db console it claims column does not exist. Model is blank(just definition of class) and controller
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
Edit: Migration is indeed empty, rerunning the genreate migration continues to yield an empty migration
class AddPreviewColumn < ActiveRecord::Migration
def change
add_column :pages, :preview, :string
end
end
syntax:
add_column :table_name, :attribute, :type
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.