简体   繁体   English

遍历Ruby on Rails中的视图

[英]Iterating over views in Ruby on Rails

I'm a Ruby on Rails noob. 我是Ruby on Rails菜鸟。 I work through Michael Hartl's tutorial on Ruby on Rails. 我研究了Michael Hartl的Ruby on Rails 教程 So far, I'm on chapter 3 about static pages. 到目前为止,我在关于静态页面的第3章中。 So far, I have StaticPage controller with three views: home , help and about . 到目前为止,我的StaticPage控制器具有三个视图: homehelpabout The layout for the controller goes like this: 控制器的布局如下所示:

<!DOCTYPE html>
<html>
  <head>
    <title><%= yield(:title) %> | Sample App</title>
    <%= csrf_meta_tags %>

    <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
  </head>

  <body>
    <%= yield %>
  </body>
</html>

What I want to do now is to add menubar, ie list of links to all static pages. 我现在想做的是添加菜单栏,即所有静态页面的链接列表。 So far, I have something like this at <body> tag: 到目前为止,我在<body>标记中有这样的内容:

<ul>
  <% '???'.each do |page| %>
    <li>Page!</li>
  <% end %>
</ul>

I can't figure out what to put instead of '???' 我不知道要放什么而不是'???' - I need an iterator of all views of a controller. -我需要一个控制器所有视图的迭代器。 Thanks in advance. 提前致谢。

What you are asking for requires more than adding static pages. 您所需要的不只是添加静态页面。 You will need a database backed model to store dynamically created pages and a controller to work with the model actions which gives you the ability to generate collections . 您将需要一个数据库支持的model来存储动态创建的页面,并需要一个controller来处理model操作,从而使您能够生成collections

With collections you can then have something like 使用collections您可以拥有类似

<ul>
  <% '???'.each do |page| %>
    <li><%= page %></li>
  <% end %>
</ul>

Since you are working with that tutorial, what you need to render your menubar is this 由于您正在使用该教程,因此呈现菜单栏所需要的是

<ul>
  <li><%= link_to "Home", home_path %></li>
  <li><%= link_to "Help", help_path %></li>
  <li><%= link_to "About", about_path %></li>
</ul>

You can render something from the database in the controller and use it in the views. 您可以在控制器中的数据库中呈现某些内容,并在视图中使用它。 Like in the controller, I can write @ones = Something.all in a method called one and if I have a show method in the controller who shows one record from the database depending on the argument it takes, then I can add a link as below and it will take me to something_show_path/:something_id . 像在控制器中一样,我可以在一个名为one的方法中编写@ones = Something.all ,如果我在控制器中有一个show方法,该方法根据所使用的参数显示数据库中的一条记录,则可以添加一个链接,如下所示:在下面,它将带我到something_show_path/:something_id

<% @ones.each do |one|%>
    <%= link_to "#{one}", something_show_path(@one) %>
<% end %>

But in this case, there is no record of the static pages you have in the database. 但是在这种情况下,数据库中没有静态页面的记录。 So, if you want a syntax like that in the views, add them in an array in the controller like 因此,如果您想要在视图中使用类似的语法,则将它们添加到控制器的数组中,例如

def whatever 
    ```rest of the code````
    @pages = ["home", "about", "help"]
end

And in the view, you can write 在视图中,您可以编写

<ul>
    <% @pages.each do |p| %>
        <li><%= link_to("#{p}", :controller => 'static_page', :action => "#{p}") %></li>
    <% end %>
</ul>

Edit for the question in the comment 编辑评论中的问题

class StaticPagesController < ApplicationController

   before_filter :load_pages, :only => [:about, :home, :help]

   ``` All your controller codes ```
    private
    def load_pages 
        @pages = ["load", "about", "help"]
    end
 end

So that all the methods in the :only list will have @pages loaded before rendering the page. 这样, :only列表中的所有方法都将在呈现页面之前加载@pages。 You don't have to load it in each method. 您不必在每种方法中都加载它。 you can declare the method in the ApplicationController by writing a private before it like I did it here and just call it in any controller with the before_filter . 您可以在ApplicationController声明一个方法,方法是像在此之前编写私有方法一样,先编写一个私有方法,然后在任何控制器中使用before_filter对其进行调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM