Reputation: 196
I'm using ruby on rails and I have some problem with it !
I tried to create a database but it seems not to work ! I generated a model and a database file thank's to the command:
rails g model photos
And here my codes
photos_controller.rb :
class PhotosController < ApplicationController
# POST /photos
# POST /photos.json
def create
@photo = Photo.new(photo_params)
photo_controller.rb
respond_to do |format|
if @photo.save
format.html { redirect_to @photo, notice: 'Photo was successfully created.' }
format.json { render action: 'show', status: :created, location: @photo }
else
format.html { render action: 'new' }
format.json { render json: @photo.errors, status: :unprocessable_entity }
end
end
end
# Never trust parameters from the scary internet, only allow the white list through.
def photo_params
params.require(:photo).permit(:image)
end
end
in the model photo.rb :
class Photo < ActiveRecord::Base
has_attached_file :image
end
in the file 2011234116731_create_photos.rb :
class CreatePhotos < ActiveRecord::Migration
def self.up
add_column :photos, :image_file_name, :string
add_column :photos, :image_content_type, :string
add_column :photos, :image_file_size, :string
add_column :photos, :image_update_at, :string
end
def self.down
remove_column :photos, :image_file_name, :string
remove_column :photos, :image_content_type, :string
remove_column :photos, :image_file_size, :string
remove_column :photos, :image_update_at, :string
end
end
But when I try to load a page which use an element "image" of the model, I ha the following error :
Photo model missing required attr_accessor for 'image_file_name' Extracted source (around line #27):
def create @photo = Photo.new(photo_params) respond_to do |format| if @photo.save
I noticed that the migration seems not to work because in my scheme.rb : ( I've done the rake db:migrate command )
ActiveRecord::Schema.define(version: 20131124183207) do
create_table "photos", force: true do |t|
t.datetime "created_at"
t.datetime "updated_at"
end
end
Upvotes: 1
Views: 822
Reputation: 13521
Doesn't seem like the migration ran correctly. And it also doesn't look like a migration that would get generated by the rails g model
command. It's missing the create_table
method. It looks like you previously created the Photo model and then created another migration to add the image fields.
My first hunch would be to try rolling the migration back:
rake db:migrate:down VERSION=2011234116731
Then running rake db:migrate
again and check your schema file to make sure all the columns are there.
Upvotes: 1