【问题标题】:Views in ruby on railsruby on rails 中的视图
【发布时间】:2016-12-09 15:43:16
【问题描述】:

我正在学习在 ruby​​ on rails 环境中编写代码。我创建了两个模型汽车和产品。我想要一个主页,其中包含指向汽车和产品的链接。单击它们中的每一个时,它们应该显示自己的视图页面。以下分别是汽车和用户的查看页面:

app/views/cars/index.html.erb

<p id="notice"><%= notice %></p>

<h1>Listing Cars</h1>

<table>
  <thead>
    <tr>
      <th>Make</th>
      <th>Color</th>
      <th>Year</th>
      <th colspan="24"></th>
    </tr>
  </thead>

  <tbody>
    <% @cars.each do |car| %>
      <tr>
        <td><%= car.make %></td>
        <td><%= car.color %></td>
        <td><%= car.year %></td>
        <td><%= link_to 'Show', car %></td>
        <td><%= link_to 'Edit', edit_car_path(car) %></td>
        <td><%= link_to 'Destroy', car, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New Car', new_car_path %>

<h4>Import that data!</h4>
  <%= form_tag import_users_path, multipart: true do %>
    <%= file_field_tag :file %>
    <%= submit_tag "Import CSV" %>
  <% end %>
</div>

app/views/users/index.html.erb

<p id="notice"><%= notice %></p>

<h1>Listing Users</h1>

<table>
  <thead>
    <tr>
      <th>User</th>
      <th>Steps</th>
      <th>Distance</th>
      <th>Minutes Exercised</th>
      <th>Hours of Sleep</th>
      <th>Calories Burned</th>
      <th colspan="24"></th>
    </tr>
  </thead>

  <tbody>
    <% @users.each do |user| %>
      <tr>
        <td><%= user.user %></td>
        <td><%= user.steps %></td>
        <td><%= user.distance %></td>
        <td><%= user.exercise %></td>
        <td><%= user.sleep %></td>
        <td><%= user.calories %></td>
        <td><%= link_to 'Show', user %></td>
        <td><%= link_to 'Edit', edit_user_path(user) %></td>
        <td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New User', new_user_path %>
<div>

<h4>Import that data!</h4>
  <%= form_tag import_users_path, multipart: true do %>
    <%= file_field_tag :file %>
    <%= submit_tag "Import CSV" %>
  <% end %>
</div>

<%= form_tag import_users_path, multipart: true, class: 'form-inline' do %>
  <div class="form-group">
    <%= link_to "Export CSV", users_path(format: "csv"), class: 'btn btn-primary' %>
  </div>

<% end %>

我不知道如何创建单个主页并提供指向这些视图的链接。一个例子会帮助我。在此先感谢各位。

【问题讨论】:

  • 您想创建静态页面吗?一个主页,您可以在其中链接到每个模型索引视图?

标签: ruby-on-rails ruby ruby-on-rails-3


【解决方案1】:

假设你有一个名为 home 的主页

在你的 home.html.erb 中写

<%= link_to "cars" cars_path %>
<%= link_to "users" users_path %>

【讨论】:

    【解决方案2】:

    你可能首先应该使用rails scaffold来看看它生成的功能页面。

    【讨论】:

      【解决方案3】:

      试试这个.......

      <%=link_to "cars" cars_path(car) %>
      
      <%=link_to "users" users_path(user) %>
      

      此链接将向您发送控制器(用户/汽车)的显示方法。然后你需要在你的views/cars/show.html.erbviews/users/show.html.erb创建一个show page然后你就可以展示他们自己的view/show页面了。

      希望这对你有用。

      【讨论】:

        【解决方案4】:

        你需要了解rails中routes的概念。

        在您的项目文件夹中运行命令rake routes。你会看到这样的映射。

                  Prefix Verb   URI Pattern                            Controller#Action
               companies GET    /companies(.:format)                   companies#index
        

        因此根据路线,以下链接将导致公司控制器的索引操作。

        <%= link_to 'Companies', companies_path%>
        

        如您所见,在companies_path 中,companies 是路由中显示的前缀。

        对于您的具体情况,以下链接将起作用。在你的 home.html.erb

        <%=link_to "All Cars", cars_path%>
        <%=link_to "All Users", users_path%>
        

        请记住,“所有用户”和 users_path 之间有逗号。因为 link_to 只是一个有多个参数的方法。

        欲了解更多信息,

        http://apidock.com/rails/v4.0.2/ActionView/Helpers/UrlHelper/link_to

        【讨论】:

          猜你喜欢
          • 2011-08-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-11-20
          • 1970-01-01
          相关资源
          最近更新 更多