我有这条线在我的票控制器:重定向不工作
before_filter :authenticate
所以,当我投票,而注销,应该失败这个过滤器。
在我的应用程序控制器我的身份验证方法是这样的:
def authenticate
logged_in? ? true : access_denied
end
所以它返回access_denied
看起来像这样:
def access_denied
redirect_to login_path, :notice => "You must log in to perform this action." and return false
end
所以,我应该被重定向到我的登录页面,向右?
我有这个在我的routes.rb文件:
match '/login' => "sessions#new", :as => "login"
,我有一个new
视图与登录表单,我应该被重定向到时候我投票,我没有登录我也。在我的票控制器的create
方法验证码:
if @vote.save
respond_to do |format|
format.html { redirect_to @video }
format.js
end
else
respond_to do |format|
format.html
end
end
但是,当我投票,我不重定向,并且我得到了我的日志此错误消息:
Started GET "/login" for 127.0.0.1 at Thu Mar 24 21:18:08 -0700 2011
Processing by SessionsController#new as JS
Completed in 17ms
ActionView::MissingTemplate (Missing template sessions/new with {:locale=>[:en, :en], :formats=>[:js, "*/*"], :handlers=>[:rhtml, :rxml, :erb, :builder, :rjs]} in view paths "/rubyprograms/dreamstill/app/views"):
为什么会发生这种情况,以及如何才能使此重定向工作?
我已经更新了我的答案和更多的细节以及简单的重定向解决方案。 – 2011-03-25 01:39:40
为什么我需要用于新会话的js文件?我不明白为什么我需要它,它应该包含什么内容......为什么当'@ vote.save'失败时,我不能仅仅使用正常的HTTP请求呢? – 2011-03-25 01:41:02
如果请求是作为来自浏览器的Javascript请求发出的,例如通过AJAX请求发出,那么您无法用HTML内容响应,因为浏览器不知道如何处理响应。您需要使用Javascript进行响应,如我的示例代码,可以使用浏览器的JavaScript引擎运行。这就是HTTP协议的工作方式。这不是Rails特有的。 – 2011-03-25 01:45:31