jeudi 10 décembre 2015

Added column will not save to database

I created a migration file to add a column to my database. The information entered into the form is not saved. Contents of the migration file: class AddDescriptionToBreweries < ActiveRecord::Migration def change add_column :breweries, :description, :text_area end

end
Contents of the model file:
class Brewery < ActiveRecord::Base
  attr_accessible :city, :food, :name, :number_of_beers, :owner, :specialty, :state, :year_founded, :zip, :brewery_logo, :description

  validates_presence_of :name, :city, :owner, :year_founded
  validates_numericality_of :number_of_beers
   def app_params
    params.require(:brewery).permit(:food, :description)
end

after_save :store_brewery_logo

BREWERY_LOGO_STORE = File.join Rails.root, 'public', 'brewery_logo_store'

def brewery_logo=(file_data)
    unless file_data.blank?
      # store the uploaded data into a private instance variable
      @file_data = file_data
      # figure out the last part of the file name and use this as
      # the file extension. e.g. from "me.jpg" will return "jpg"
      self.extension = file_data.original_filename.split('.').last.downcase
    end
  end

  def has_brewery_logo?
    File.exists? brewery_logo_filename
  end

def brewery_logo_path
    "/brewery_logo_store/#{id}.#{extension}"
end
def brewery_logo_filename
    File.join BREWERY_LOGO_STORE, "#{id}.#{extension}"
end

private

def store_brewery_logo
        if @file_data
          # make the photo_store directory if it doesn't exist already
          FileUtils.mkdir_p BREWERY_LOGO_STORE
          # write out the image data to the file
          File.open(brewery_logo_filename, 'wb') do |f|
            f.write(@file_data.read)
        end
          # avoid repeat-saving
          @file_data = nil
        end
    end
end

Contents of the Brewery Controller:
class BreweriesController < ApplicationController
  # GET /breweries
  # GET /breweries.json
  def index
    @breweries = Brewery.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @breweries }
    end
  end

  # GET /breweries/1
  # GET /breweries/1.json
  def show
    @brewery = Brewery.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @brewery }
    end
  end

  # GET /breweries/new
  # GET /breweries/new.json
  def new
    @brewery = Brewery.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @brewery }
    end
  end

  # GET /breweries/1/edit
  def edit
    @brewery = Brewery.find(params[:id])
  end


  # POST /breweries
  # POST /breweries.json
  def create
    @brewery = Brewery.new(params[:brewery])

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


  # PUT /breweries/1
  # PUT /breweries/1.json
  def update
    @brewery = Brewery.find(params[:id])


    respond_to do |format|
      if @brewery.update_attributes(params[:brewery])
        format.html { redirect_to @brewery, notice: 'Brewery was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @brewery.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /breweries/1
  # DELETE /breweries/1.json
  def destroy
    @brewery = Brewery.find(params[:id])
    @brewery.destroy

    respond_to do |format|
      format.html { redirect_to breweries_url }
      format.json { head :no_content }
    end
  end
end

Aucun commentaire:

Enregistrer un commentaire