English 中文(简体)
Rails:为同一个URL显示不同的视图?
原标题:Rails: Displaying different view for the same URL?

首先,举一个我想做的事情的例子:

  1. If you go to http://www.meetup.com and you are not signed in, you see a page that shows Do something • Learn something... etc. which is a public page
  2. But when you are logged in, that same page (URL) shows Welcome, xxx...Whats happening... etc. which is specific to you.
    That is what I m trying to do in my app as well.

如何在Rails2.3.8中做到这一点?

到目前为止,我已经:

  1. An AboutsController intended to serve up semi-static pages (wish the About wasn t plural!)
  2. Root route is map.root => :controller => "about".

现在,当未登录的用户转到时http://www.abc.com,他将获得视图关于/index的内容。到目前为止还不错。

但是,当用户登录时,我希望产品/索引视图应显示为相同的URL,即http://www.example.comURL(而不是http://www.example.com/products

这在Rails 2.3.8中可能吗?

最佳回答

做到这一点最干净的方法是使用一种叫做before_filter的东西。它是位于控制器顶部的一个命令,在该控制器(您需要的)中执行任何操作之前都会调用该命令。通常,您需要对控制器中的多个操作进行相同的检查,因此将该检查直接放入每个操作中是没有意义的。

假设您有一个Comments控制器,并且您希望编辑、更新和销毁操作只有登录用户才能执行。这是非常常见的。让我们看一个例子。为了简洁起见,我不会详细说明控制器的所有操作,只列出一些独特的内容:

class CommentsController < ApplicationController
  before_filter :user_logged_in?, :only => [:edit, :update, :destroy]

  # all your actions would go here - index, show, etc #

  protected

  def user_logged_in?
    redirect_to dashboard_path unless current_user
  end
end

在上面的示例中,user_loged_In方法将在运行编辑、更新或销毁操作之前运行。如果在该方法内部调用了render或redirect,rails将短暂停止并且永远不会运行该操作,这就是它被称为过滤器的原因。相反,将遵守给定的渲染或重定向请求。

current_user方法是大多数用户身份验证插件提供的常见帮助程序,如果没有当前用户,该方法通常为零。因此,我们的过滤方法是告诉rails重定向到某个路径,,除非用户登录。

这是事实上的铁路方式来处理这样的事情。问得很好。

问题回答

这当然是可能的。可以有条件地渲染任何需要的视图,如下所示:

 def index
   if current_user
     render :action =>  products , :controller =>  index 
   else
     render :action =>  index , :controller =>  about 
   end
 end

假设你正在验证Authlogic,Devise。无论使用什么逻辑来确定用户是否登录,都会进入条件。

我见过很多网站通过重定向到/dashboard来处理这一问题,以保持其应用程序内部尽可能干净。无需过于担心它仍然是根URL,尽管这显然是可能的,正如其他解决方案所示:)





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签