我有一個社交網絡,我正在使用載波。我需要設置它的幫助,以便用戶不必創建圖片庫名稱來上傳照片。這是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
你最後的例子是我正在尋找的方法。你的工作看起來很簡單,我理解這些變化,然而它給出了一個未定義的方法來控制照片創建的每一個。 – pwz2000 2013-04-26 16:14:12
你究竟在哪裏得到這個錯誤?同時重定向到current_user.photos?我認爲它必須是user_photos_path(current_user),而不是current_user.photos,如果你設置你的用戶照片路由嵌套..sorry,我的錯誤:)。我目前更新了我的答案。 – Mattherick 2013-04-26 19:24:03
代碼打破了應用程序。未定義的方法'畫廊'的照片控制器銷燬行動。當我刪除照片時會發生這種情況。當我訪問頁面http:// localhost:3000/photos/new?gallery_id = 1並選擇要上傳的圖片然後提交時,每個都會發生未定義的方法。它將我重定向到http:// localhost:3000/photos。因此,錯誤來自創建操作。順便說一句,我已經下了路線已經。我剛剛用我的路線編輯原始帖子。 – pwz2000 2013-04-29 12:46:33