我嘗試做以下操作: 用戶在他的個人資料頁面上。現在他編輯他的個人資料。他點擊更新並保存數據。 現在我想將用戶重定向到另一種配置文件編輯頁面。 我做我的users_controller.rb如下:Rails:在Controller中添加新的Action,但沒有路徑?
def update
@user = User.find(params[:id])
respond_to do |format|
if @user.update_attributes(params[:user])
flash[:notice] = 'User was successfully updated.'
if(@user.team_id != nil)
format.html { redirect_to(@user) }
else
format.html { redirect_to choose_team_path }
end
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end
def choose_team
@user = User.find(params[:id])
end
我創建了一個觀點:/users/choose_team.html.erb
現在我得到以下錯誤:
undefined local variable or method `choose_team_path' for #<UsersController:0x1f56650>
所以我在我的routes.rb添加了choose_team:
map.choose_team 'choose-team', :controller => 'users', :action => 'choose_team'
現在,在提交我的第一個編輯形式,它重定向我http://localhost:3000/choose-team
,我得到以下錯誤:Couldn't find User without an ID
我想要什麼: 如果用戶沒有TEAM_ID,他應該被重定向到我choose_team.html.erb選購隊,否則他應該被重定向到他的個人資料/節目。 如何做到這一點?
編輯: 通過編寫format.html { redirect_to :action => "choose_team" }
而不是format.html { redirect_to choose_team_path }
它工作。但是,我怎麼能得到像/用戶/ 1/choose_team的網址?目前它只是/ choose_team。
非常感謝! – Newbie 2010-05-31 09:43:58