2011-10-15 16 views
0

我在用戶頁面調用的notecards控制器中有一個銷燬方法來刪除記事卡。在下面的第一個例子中,重定向傳遞的是記錄卡ID導致找不到頁面,而第二個用戶ID正在通過正確傳遞查找用戶頁面..有人可以幫我理解爲什麼嗎?爲什麼在這兩個例子中,一個重定向到正確的URL而另一個不是?

重定向到的notecard

def destroy 
    @note = Notecard.find_by_id(params[:id]) 
    delete_note(@note) 
    redirect_to user_path(@current_user) 
end 

用戶通過ID重定向到用戶傳遞的用戶ID

def destroy 
    @note = current_user.notecards.find_by_id(params[:id]) 
    delete_note(@note) 
redirect_to user_path(@current_user) 
end 

更新:

感謝您的答覆。代碼如下:https://github.com/incorvia/plumnotes/ ..身份驗證位於會話助手和會話控制器中。至於日誌:

隨着Notecard.find_by_id(PARAMS [:編號])

Started DELETE "/notecards/177" for 127.0.0.1 at 2011-10-15 11:53:38 -0400 
    Processing by NotecardsController#destroy as HTML 
    Parameters: {"authenticity_token"=>"WctbONb/qAO+hesHZ6Yw5zU19eCPNGeILIhxnW9Pi1Y=", "id"=>"177"} 
    Notecard Load (0.3ms) SELECT `notecards`.* FROM `notecards` WHERE `notecards`.`id` = 177 LIMIT 1 
    SQL (0.8ms) DELETE FROM `notecards` WHERE `notecards`.`id` = 177 
Redirected to http://localhost:3000/users/177 
Completed 302 Found in 12ms 


Started GET "https://stackoverflow.com/users/177" for 127.0.0.1 at 2011-10-15 11:53:39 -0400 
    Processing by UsersController#show as HTML 
    Parameters: {"id"=>"177"} 
    User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 9 LIMIT 1 
    User Load (0.6ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 177 LIMIT 1 
Completed 404 Not Found in 14ms 

ActiveRecord::RecordNotFound (Couldn't find User with id=177): 
app/controllers/users_controller.rb:11:in `show' 

Rendered /Users/Kevin/.rvm/gems/[email protected]/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.4ms) 
Rendered /Users/Kevin/.rvm/gems/[email protected]/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms) 
Rendered /Users/Kevin/.rvm/gems/[email protected]/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (4.6ms) 

隨着current_user.notecards.find_by_id(PARAMS [:編號])

Started DELETE "/notecards/179" for 127.0.0.1 at 2011-10-15 11:56:18 -0400 
    Processing by NotecardsController#destroy as HTML 
    Parameters: {"authenticity_token"=>"WctbONb/qAO+hesHZ6Yw5zU19eCPNGeILIhxnW9Pi1Y=", "id"=>"179"} 
    User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 9 LIMIT 1 
    Notecard Load (0.6ms) SELECT `notecards`.* FROM `notecards` WHERE `notecards`.`user_id` = 9 AND `notecards`.`id` = 179 LIMIT 1 
    SQL (0.9ms) DELETE FROM `notecards` WHERE `notecards`.`id` = 179 
Redirected to http://localhost:3000/users/9 
Completed 302 Found in 95ms 


Started GET "https://stackoverflow.com/users/9" for 127.0.0.1 at 2011-10-15 11:56:18 -0400 
    Processing by UsersController#show as HTML 
    Parameters: {"id"=>"9"} 
+0

聽起來像你的'current_user'沒有被正確定義。您應該在登錄時只定義一次,並且絕不會在其他地方使用 – iwasrobbed

+0

請更新您的問題,其中包括兩個方面:1)您的current_user方法,以及2)兩種情況下的控制檯輸出。 – bricker

回答

0

似乎@current_user實際上並沒有被用於@Notcard控制器定義,除非CURRENT_USER方法被調用這就是爲什麼current_user.notecards ...工作 它似乎也與user_path(@current_user)..如果@current_user沒有定義,那麼它只是從params的ID ..這恰好是notecard id,而不是用戶ID ... 修復它的解決方案是用戶current_user.notecards ..或爲控制器添加一個before_filter授權用戶並定義@current_user實例va riable :-)

1

難以猜測什麼是定義到本地var @current_user和它發生的地方。 如果你真的感興趣,爲什麼 - 可能更多的代碼可能會有所幫助。

有時候,保持簡單可能是一個非常有趣的想法(:

redirect_to user_path(current_user.id) 
相關問題