0

I have a social network and for it I am using carrier wave. I need help with setting it up so that users do not have to create a Gallery name to upload photos. That's the default setup for carrierwave, but since this is a social network I am not allowing users to have different Galleries (or albums). There should only be a link to upload photos to their profile. So how should I attack this? BTW I am new to Rails so any and all help would be greatly appreciated!

photos controller:

  def new
    @photo = Photo.new(:gallery_id => params[:gallery_id])
  end

  def create
    @photo = Photo.new(params[:photo])
    if @photo.save
      flash[:notice] = "Successfully created photos."
      redirect_to @photo.gallery
    else
      render :action => 'new'
    end
end

  def edit
    @photo = Photo.find(params[:id])
  end

  def update
    @photo = Photo.find(params[:id])
    if @photo.update_attributes(paramas[:photo])
      flash[:notice] = "Successfully updated photo."
      redirect_to @photo.gallery
    else
      render :action => 'edit'
    end
  end

  def destroy
    @photo = Photo.find(params[:id])
    @photo.destroy
    flash[:notice] = "Successfully destroyed photo."
    redirect_to @photo.gallery
  end
end

galleries controller:

  def index
    @galleries = Gallery.all
  end

  def show
    @gallery = Gallery.find(params[:id])
  end

  def new
    @gallery = Gallery.new
  end

  def create
    @gallery = Gallery.new(params[:gallery])
    if @gallery.save
      flash[:notice] = "Successfully created gallery."
      redirect_to @gallery
    else
      render :action => 'new'
    end
  end

  def edit
    @gallery = Gallery.find(params[:id])
  end

  def update
    @gallery = Gallery.find(params[:id])
    if @gallery.update_attributes(params[:gallery])
      flash[:notice] = "Successfully updated gallery."
      redirect_to gallery_url
    else
      render :action => 'edit'
    end
  end

  def destroy
    @gallery = Gallery.find(params[:id])
    @gallery.destroy
    flash[:notice] = "Successfully destroy gallery."
    redirect_to galleries_url
  end
end

routes:

Dating::Application.routes.draw do
  get 'signup' => 'users#new'
  get 'login' => 'sessions#new'
  get 'logout' => 'sessions#destroy'
  get 'edit' => 'users#edit'
  get "/profile/:id" => "users#show"
  get "profile/:id/settings" => 'users#edit'
  match 'settings/:id' => 'users#settings'

  resources :users
  resources :sessions
  resources :password_resets
  resources :galleries
  resources :photos
  resources :searches

  resources :users do  
      get 'settings', on: :member  
  end

  root to: 'users#new'
  root to: 'galleries#index'

  resources :users do |user|
    resources :messages do
      collection do
        post 'delete_multiple'
      end
    end
  end

3 Answers 3

2
+50

zwippie s answer is a good option as far as I can see.

If you follow zwippie´s answer, you have to modify your photos controller, e.g like follwoing:

def new
  @photo = Photo.new
end

def create
  @photo = Photo.new(params[:photo])
  @photo.gallery = current_user.gallery
  if @photo.save
    flash[:notice] = "Successfully created photos."
    redirect_to gallery_path @photo.gallery
  else
    render :action => 'new'
  end
end

An completely other option is not to generate any gallery, just associate the current user to the photo.

user model:

class User < ActiveRecord::Base
  has_many :photos
end

class Photos < ActiveRecord::Base
  belongs_to :user
end

photos_controller

def new 
  @photo = Photo.new
end

def create
  @photo = Photo.new(params[:photo])
  @photo.user = current_user
  if @photo.save
    flash[:notice] = "Successfully created photos."
    redirect_to user_photos_path(current_user)
  else
    render :action => 'new'
  end
end

As gallery name for your carrierwave setup you can for example choose the current user username, or something else.

UPDATE: changed redirect path

your routes should look like following:

resources :users do
  resources :photos
end
6
  • Your last example is the method I was looking for. Your work seems simple and I understand the changes, however it gives a undefined method `each' for the photos controlelr create.
    – pwz2000
    Apr 26, 2013 at 16:14
  • where exactly you got this error? while redirecting to current_user.photos? I think it must be user_photos_path(current_user) instead of current_user.photos, if you setup your user photos routes nested..sorry, my fault :). I currently updated my answer..
    – Matthias
    Apr 26, 2013 at 19:24
  • The code breaks app. undefined method `gallery' for photos controller destroy action. This happens when I delete a photo. The undefined method for each happens when I access page localhost:3000/photos/new?gallery_id=1 and select picture to upload then submit. It redirects me to localhost:3000/photos. So that error is from the create action. BTW I had that down for routes already. I just edited original post with my routes.
    – pwz2000
    Apr 29, 2013 at 12:46
  • UPDATE: Getting undefined method `each' that points to photo controller 'create' method.
    – pwz2000
    May 1, 2013 at 18:55
  • when exactly do you get this error? while redirecting? do you have :dependent => destroy in your model?
    – Matthias
    May 2, 2013 at 18:40
0

Why not create the gallery directly after the user has signed up? Or create it when the user uploads the first picture.

photos_controller:

def new
  # Get the gallery for the current user (assuming Gallery has attribute user_id)
  @gallery = current_user.gallery

  # Create a new gallery if user does not have one already
  @gallery = Gallery.create(:user_id => current_user.id) if @gallery.nil?

  # Save the photo
  @photo = Photo.new(:gallery_id => @gallery.id)
end
1
  • That gives a undefined method `gallery' for nil:NilClass when going to the gallery and then adding photo. Which I think is coming from the @photo = Photo.new. So in a nutshell I can go to upload a picture but when I click submit is when the undefined method appears.
    – pwz2000
    Apr 26, 2013 at 14:17
0

You can use paperClip to create photoalbum, here is link for paperclip gem:

https://github.com/thoughtbot/paperclip

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.