0

我有一个社交网络,为此我正在使用载波。我需要帮助进行设置,这样用户就不必创建图库名称来上传照片。这是carrierwave的默认设置,但由于这是一个社交网络,我不允许用户拥有不同的画廊(或专辑)。应该只有一个链接可以将照片上传到他们的个人资料。那么我应该如何攻击呢?顺便说一句,我是 Rails 的新手,所以任何和所有的帮助都将不胜感激!

照片控制器:

  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

画廊控制器:

  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

路线:

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
4

3 回答 3

2

据我所知,zwippie 的回答是一个不错的选择。

如果您遵循 zwippie 的回答,则必须修改您的照片控制器,例如如下所示:

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

另一种选择是不生成任何图库,只需将当前用户与照片相关联。

用户模型:

class User < ActiveRecord::Base
  has_many :photos
end

class Photos < ActiveRecord::Base
  belongs_to :user
end

照片控制器

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

作为您的载波设置的画廊名称,您可以选择当前用户用户名或其他名称。

更新:更改重定向路径

您的路线应如下所示:

resources :users do
  resources :photos
end
于 2013-04-25T17:56:43.683 回答
0

为什么不在用户注册后直接创建图库?或者在用户上传第一张图片时创建。

照片控制器:

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
于 2013-04-25T14:35:14.093 回答
0

您可以使用回形针创建相册,这里是回形针宝石的链接:

https://github.com/thoughtbot/paperclip

于 2013-05-06T17:43:45.357 回答