在同一视图中使用 2 个操作,索引和显示?

发布于 2024-12-09 10:12:10 字数 66 浏览 2 评论 0原文

我怎样才能使视图索引与同一个视图中的 2 个操作显示和索引一起使用???我是 ruby​​ on Rails 的新手。

How can i make a view index working with 2 actions show and index in the same view ??? I`m new in ruby on rails.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃素的狼 2024-12-16 10:12:10

目前尚不清楚为什么您希望 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

It's not quite clear why you'd want a show method to be the same as an index but the easiest approach is to redirect:

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

The alternative is to render the same template:

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

If you're using the render method you will have to populate the same instance variables as in the index method or your view may not be able to run. If you want to be sure that the same variables are set, you can also do this:

def show
  self.index
  render(:action => 'index')
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文