2013-04-22 72 views
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 

回答

2

zwippie的答案是一個很好的選擇,因爲據我可以看到。

如果按照zwippie's答案,你必須修改你的照片控制器,例如像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 

的完全另一種選擇是不產生任何畫廊,只是當前用戶關聯到的照片。

用戶模型:

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 

至於你carrierwave設置,您可以選擇例如當前用戶的用戶名,或別的東西畫廊的名字。

UPDATE:改變重定向路徑

你的路線應該像下面:

resources :users do 
    resources :photos 
end 
+0

你最後的例子是我正在尋找的方法。你的工作看起來很簡單,我理解這些變化,然而它給出了一個未定義的方法來控制照片創建的每一個。 – pwz2000 2013-04-26 16:14:12

+0

你究竟在哪裏得到這個錯誤?同時重定向到current_user.photos?我認爲它必須是user_photos_path(current_user),而不是current_user.photos,如果你設置你的用戶照片路由嵌套..sorry,我的錯誤:)。我目前更新了我的答案。 – Mattherick 2013-04-26 19:24:03

+0

代碼打破了應用程序。未定義的方法'畫廊'的照片控制器銷燬行動。當我刪除照片時會發生這種情況。當我訪問頁面http:// localhost:3000/photos/new?gallery_id = 1並選擇要上傳的圖片然後提交時,每個都會發生未定義的方法。它將我重定向到http:// localhost:3000/photos。因此,錯誤來自創建操作。順便說一句,我已經下了路線已經。我剛剛用我的路線編輯原始帖子。 – pwz2000 2013-04-29 12:46:33

0

爲什麼不創建用戶簽約後直接畫廊?或者當用戶上傳第一張照片時創建它。

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 
+0

這給了一個未定義的方法'畫廊'爲零:NilClass當去畫廊,然後加入g照片。我認爲這是來自@photo = Photo.new。所以簡而言之,我可以去上傳一張圖片,但是當我點擊提交時,就是未定義的方法出現的時候。 – pwz2000 2013-04-26 14:17:19