In order to be able to manage a handful of sample images on my rails app's homepage I created an image uploader using CarrierWave.
The problem I'm having is my image_uploader's controller isn't accessible to the homepage at root 'static_pages#home'
. Is there a way I can make the uploader's controller accessible to my (mostly) static 'static_pages#home'?
For now the only way I could get it to work is by moving my home page index from /app/views/static_pages/home.html.erb
to /app/views/covers/index.html.erb
and changing the route, but this seems like a poor workaround.
Thanks for any help.
Update:
As @Udaykumardas suggested below I created a app/models/static_page.rb
file containing
class Cover < ActiveRecord::Base
mount_uploader :photo, PhotoUploader # Tells rails to use this uploader for this model.
end
but now i get the following error:
NoMethodError in StaticPagesController#home
undefined method `each' for nil:NilClass
referring to <% @covers.each do |cover| %>
Here is my routes info in case it helps
get 'covers/index'
get 'covers/new'
get 'covers/create'
get 'covers/destroy'
get 'password_resets/new'
get 'password_resets/edit'
get 'account_activations/edit'
get 'sessions/new'
root 'static_pages#home'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :microposts, only: [:create, :destroy]
resources :covers, only: [:index, :new, :create, :destroy]
I'm unsure if the bottom line needs to be changed to resources :static_pages, only: [:index, :new, :create, :destroy]
. I have tried this and it results in a different error as follows:
NameError in StaticPagesController#home
undefined local variable or method `new_cover_path'
Thanks again for any help.
Use
mount_uploader :image, ImageUploader
inStaticPage.rb
file.