Ruby on rails 为什么在这两个示例中,一个是重定向到正确的URL,而另一个不是';T
我在notecards控制器中有一个destroy方法,我正在从用户页面调用该方法来删除notecard。在下面的第一个示例中。。重定向正在传递notecard ID,导致找不到页面,而第二个重定向正在正确传递用户ID以查找用户页面。。有人能帮我理解为什么吗 重定向到传递记事卡id的用户 重定向到传递用户id的用户 更新: 谢谢你的回复。代码在这里:。。身份验证位于会话帮助器和会话控制器中。至于日志: 使用记事卡。通过\u id查找\u(参数[:id]) 使用当前用户.notecards.find\u by_id(params[:id])Ruby on rails 为什么在这两个示例中,一个是重定向到正确的URL,而另一个不是';T,ruby-on-rails,ruby-on-rails-3,redirect,parameters,Ruby On Rails,Ruby On Rails 3,Redirect,Parameters,我在notecards控制器中有一个destroy方法,我正在从用户页面调用该方法来删除notecard。在下面的第一个示例中。。重定向正在传递notecard ID,导致找不到页面,而第二个重定向正在正确传递用户ID以查找用户页面。。有人能帮我理解为什么吗 重定向到传递记事卡id的用户 重定向到传递用户id的用户 更新: 谢谢你的回复。代码在这里:。。身份验证位于会话帮助器和会话控制器中。至于日志: 使用记事卡。通过\u id查找\u(参数[:id]) 使用当前用户.notecards.fin
很难猜测本地var@current_用户定义了什么以及它发生在哪里。 如果你真的感兴趣,为什么,-也许更多的代码会有帮助 有时候,保持简单可能是一个非常有趣的想法(:
除非调用了current_user方法,否则@current_user实际上没有为@Notcard控制器定义。这就是为什么current_user.notecards…起作用的原因 同样,对于user_path(@current_user)…如果未定义@current_user,那么它只从参数中获取id。这恰好是记事卡id,而不是用户id。。。
修复它的解决方案是要么使用当前用户.notecards..,要么为控制器添加一个before\u过滤器,该过滤器授权用户并定义@current\u user实例变量:-)听起来您的
当前用户
定义不正确。您应该在登录时只定义一次,而不要在任何地方定义。请用两件事更新您的问题:1)您当前的用户方法,以及2)这两种情况下的控制台输出。
def destroy
@note = Notecard.find_by_id(params[:id])
delete_note(@note)
redirect_to user_path(@current_user)
end
def destroy
@note = current_user.notecards.find_by_id(params[:id])
delete_note(@note)
redirect_to user_path(@current_user)
end
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 "/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/ruby-1.9.2-p290@notes/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.4ms)
Rendered /Users/Kevin/.rvm/gems/ruby-1.9.2-p290@notes/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms)
Rendered /Users/Kevin/.rvm/gems/ruby-1.9.2-p290@notes/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (4.6ms)
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 "/users/9" for 127.0.0.1 at 2011-10-15 11:56:18 -0400
Processing by UsersController#show as HTML
Parameters: {"id"=>"9"}
redirect_to user_path( current_user.id )