重定向不起作用 [英] Redirect not working

查看:71
本文介绍了重定向不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的投票控制器中有这一行:

I have this line in my votes controller:

before_filter :authenticate

因此,当我在注销时投票时,它应该无法通过此过滤器.

So when I vote while logged out, it should fail this filter.

我的应用程序控制器中的身份验证方法如下所示:

My authenticate method in my application controller looks like this:

def authenticate
    logged_in? ? true : access_denied
end

所以它返回 access_denied 看起来像这样:

So it returns access_denied which looks like this:

def access_denied
    redirect_to login_path, :notice => "You must log in to perform this action." and return false
end

因此,我应该被重定向到我的登录页面,对吗?

Therefore, I should be redirected to my login page, right?

我的 routes.rb 文件中有这个:

I have this in my routes.rb file:

match '/login' => "sessions#new", :as => "login"

我有一个带有登录表单的 new 视图,当我投票但我没有登录时,我应该重定向到它.我在 create我的投票控制器的方法:

and I have a new view with a login form that I should be redirected to when I vote and I'm not logged in. I also have this code in the create method of my votes controller:

if @vote.save
    respond_to do |format|
      format.html { redirect_to @video }
      format.js
    end
  else
    respond_to do |format|
      format.html
    end
  end

但是当我投票时,我没有被重定向,并且我在我的日志中收到了这个错误消息:

But when I vote, I am not redirected, and I get this error message in my logs:

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"):

为什么会发生这种情况,我如何才能使重定向工作?

Why is this happening, and how can I make this redirect work?

推荐答案

您似乎在向 VotesController#create 操作发出 javascript 请求,但缺少 Sessions#new 的 JS 模板.

It seems like you are making a javascript request to your VotesController#create action, but you are missing a JS template for Sessions#new.

这是发生的事情:您将 VotesController#create 作为 JS 调用进行调用,当用户登录时一切正常,因为 VotesController#create 响应 javascript 操作.问题是如果用户没有登录,请求重定向到SessionsController#new,请求仍然是javascript请求.问题是 SessionsController#new 不响应 JS,所以你会得到一个缺少模板的错误.

Here's what happens: you make a call to VotesController#create as a JS call, and when the user is logged in all is fine, because VotesController#create response to javascript actions. The problem is that if the user is not logged in, the request redirects to SessionsController#new, and the request is still a javascript request. The problem is that SessionsController#new does not respond to JS so you get a missing template error.

我建议添加一个 app/views/sessions/new.js.erb 文件来处理 JS 调用.您现在可能只有一个 app/views/sessions/new.html.erb 文件.

I would recommend adding an app/views/sessions/new.js.erb file to handle the JS call. You probably only have an app/views/sessions/new.html.erb file right now.

您也可以通过使用 javascript 重定向到 HTML 请求来处理这种情况.您可以通过将以下内容添加到 new.js.erb 模板来实现:

You can also handle this case by redirecting, using javascript, to an HTML request. You can do this by adding the following to your new.js.erb template:

window.location = "<%= escape_javascript(login_path) %>";

这基本上会告诉浏览器重定向到 login_path,就好像请求是 HTML 请求,而不是 Javascript 请求.

This will basically tell the browser to redirect to the login_path as if the request were an HTML request, instead of a Javascript request.

这篇关于重定向不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆