Rails:重定向两个页面 [英] Rails: redirect two pages back

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

问题描述

我正在努力解决这个问题:

I'm trying to get my head around this one:

假设您有两个模型,其中:

Say you have two models where:

:bar has_many :foos

:bar has_many :foos

你有一个这样的网址:http://myapp.com/24-name-of-the-bar-to-param/foos/new

在我的网站上,此页面显示了许多有关用户将为其创建 foo 的栏的信息.因此,即使用户未登录,用户仍然可以看到信息.

On my site this page shows a lot of information about the bar which users are going to create a foo for. So, even if a user isn't logged in the user will still be able to see the info.

目前,当用户登录时,一个用于创建新 foo 的表单位于网页的左侧.当用户未登录时,它会显示请登录或注册"

Currently, when the user is logged in, a form to create a new foo is on the left hand side of the web page. When the user isn't logged in it says "Please login or register"

该表单解释了很多关于我的应用程序如何工作的信息,因此我想对其进行更改,以便即使用户未登录该表单也会显示,如果他们单击提交,它将带他们到 login_path然后当他们登录时,回到提交表单的路径.

The form explains a lot about how my app works, so I'd like to change it so that even if a user isn't logged in the form will display, and if they click submit it will take them to the login_path and then when they login, back to the path where the submitted the form.

我遇到了这个问题:目前我的应用程序控制器中有一个 login_required 方法,如下所示:

I'm running into this problem: Currently I have a login_required method in my application controller like this:

    def store_location
            session[:return_to] = request.request_uri
    end
 def login_required
  unless current_user || admin?
      store_location
      flash[:notice] = "Please log in"
      redirect_to login_path and return false
  end
 end

这个需要登录的动作是在 foo 的创建动作上调用的.当我点击表单上的提交时,我会转到 http://myapp.com/foos 而不是 http://myapp.com/24-name-of-the-bar-to-param/foos/new

This login required action is called on the create action of the foo. When I click submit on the form it takes me to http://myapp.com/foos instead of http://myapp.com/24-name-of-the-bar-to-param/foos/new

我认为这是因为需要登录的函数是在 create 操作而不是 new 操作上调用的.

I assume this is because the login required function is called on the create action and not the new action.

有什么想法吗?

根据请求更新这里是控制器代码和回调:

UPDATE as per request here is the controller code and callbacks:

  before_filter :find_bar, :except => [:index, :edit, :update]
  before_filter :login_required, :only => [:create]
  ssl_required :edit, :update


  def new
   @foo = Foo.new :amount =>  "0.00"
   @foos = Foo.find(:all, :conditions => ["bar_id = ?", @bar.id], :order => "created_at DESC").paginate :page => params[:page], :per_page => 10
   @foos_all = Foo.find(:all, :conditions => ["hatlink_id = ?", @hatlink.id], :order => "created_at DESC")
   @current_user = current_user
   @topfooers = User.bar_amount(@bar, nil)
   @average_foo = @bar.foos.average('amount')
  end

  def create
   @foo = @current_user.foos.build params[:foo]
   if (@bar.foos << @foo)
    flash[:notice] = "Thank you for fooing!"
    redirect_to new_bar_foo_path(@bar)
   else
    render :action => :new
   end
  end
private
  def find_bar
 @bar_id = params[:bar_id]
 return(redirect_to(categories_path)) unless @bar_id
 @bar = Bar.find(@bar_id)
  end

推荐答案

在发布问题后仅五分钟就想出解决方案的我真是太傻了.哦,这就是我所做的(并且有效).

Silly of me to come up with a solution just five minutes after posting the question. Oh well, here's what I did (and it works).

在 foo 的新"操作中,我添加了这些行

In the "new" action of foo I added these lines

if !current_user
    store_location
end

在需要登录的方法中,我添加了这个:

In the login required method I have added this:

if params[:controller] == "foos" && params[:action] == "create"
                #Took out the line for storing the location in this method.
                    flash[:notice] = "Please log in"
                redirect_to login_path and return false

这篇关于Rails:重定向两个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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