pwz2000
pwz2000

Reputation: 1395

Allow users to upload photos bypassing creating gallery

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

Upvotes: 0

Views: 726

Answers (3)

PradeepGupta
PradeepGupta

Reputation: 23

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

https://github.com/thoughtbot/paperclip

Upvotes: 0

Matthias
Matthias

Reputation: 4375

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

Upvotes: 2

zwippie
zwippie

Reputation: 15525

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

Upvotes: 0

Related Questions