【问题标题】:Using 2 actions in the same view, index and show ?在同一个视图、索引和显示中使用 2 个操作?
【发布时间】:2011-10-13 14:40:58
【问题描述】:

如何使视图索引在同一个视图中显示和索引 2 个操作?我是 ruby​​ on rails 的新手。

【问题讨论】:

  • 使用redirect_torender
  • 我尝试使用渲染,但在索引中我有一个侧边栏,其中列出了新闻,并且在页面中心我需要显示带有 id 的通知,如果我使用渲染我必须在 show.html.haml 和 index.hatml.haml 中复制我的代码。没有别的办法???

标签: ruby-on-rails ruby controller action


【解决方案1】:

尚不清楚为什么您希望 show 方法与 index 相同,但最简单的方法是重定向:

def show
  redirect_to(:action => :index)
end

另一种方法是渲染相同的模板:

def show
  render(:action => 'index')
end

如果您使用render 方法,则必须填充与index 方法相同的实例变量,否则您的视图可能无法运行。如果你想确保设置了相同的变量,你也可以这样做:

def show
  self.index
  render(:action => 'index')
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-30
    • 1970-01-01
    • 2014-10-02
    相关资源
    最近更新 更多