【问题标题】:can I get public/index.html to work alongside another root_path?我可以让 public/index.html 与另一个 root_path 一起工作吗?
【发布时间】:2013-05-04 00:41:39
【问题描述】:

我刚刚使用 public/index.html 实现了一个新主页,我读到它覆盖了很多路由。

我最初拥有root to: static_pages#home,如果用户已登录,则在我的static_pages\home.html.erb 视图中,如果用户未登录,他们会看到已登录的主页和匿名访问者 (public/index.html) 主页。

在实现 public/index.html 后,我为登录用户创建了一条新路由,并将之前的 root_path 重定向到 home_path

get '/home' => 'static_pages#home', :as => :home

但是,我想使用http://localhost:3000 作为登录和访问者主页的主页。这可能吗?如何让访问者看到当前运行良好的当前 public/index.html,但登录后不必使用http://localhost:3000/home?登录后我也想要http://localhost:3000

谢谢

【问题讨论】:

  • 您需要对其进行编码或查找某人制作的过滤器,该过滤器会根据登录的 cookie 状态更改包含的内容。

标签: ruby-on-rails ruby routes public-html


【解决方案1】:

public/index.html 在 Rails 之前由网络服务器提供服务,因此您需要另一种解决方案。

相反,您可以将public/index.html 移动到app/views/static_pages/index.html 并按如下方式编辑您的控制器:

class StaticPagesController < ApplicationController
  def home 
    if signed_in?
      # current content of #home action
    else
      render :index, :layout => false
    end
  end
end

甚至更干净的方式

class StaticPagesController < ApplicationController
  before_filter :show_index_page, :only => :home

  def home 
    # the same
  end

private
  def show_index_page
    render :index, :layout => false unless signed_in?
  end
end

【讨论】:

  • 如果我回到原来的 home.html.erb 方法,我有 &lt;% if signed_in? %&gt;&lt;% else %&gt;,是否有代码或其他东西可以用来制作 &lt;% else %&gt; 以下的所有内容没有现有的格式/没有其他任何东西?看来,当我将当前在 public/index.html 中的内容复制并粘贴到那里时,格式已全部关闭,所以我想知道是否可以将代码包含在某些东西周围
【解决方案2】:

您需要的是一个简单的 HomeController,它为相应的用户呈现正确的视图。

您必须有两个匿名用户和登录用户的 erb 视图文件。

【讨论】:

  • HomeController 应该如何呈现正确的视图?只是class StaticPagesControllerdef homeif signed_in?这里有什么else这里有end吗?
猜你喜欢
  • 2021-12-03
  • 1970-01-01
  • 1970-01-01
  • 2021-01-13
  • 1970-01-01
  • 1970-01-01
  • 2011-06-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多