【问题标题】:how do I link pages on rails?如何在 Rails 上链接页面?
【发布时间】:2013-08-26 22:56:37
【问题描述】:

我为我制作的 demo_app 提供了三个资源。我做了一个主页,但我想把主页的链接链接到我的三个资源。我把代码放出来了

<%= link_to 'users', @user %> | 
<%= link_to 'microposts', @micropost %> |
<%= link_to 'task', @task %> |

在我的主页上,链接显示在页面上,但它们不起作用

routes.rb 文件

DemoApp::Application.routes.draw do
      get "static_pages/home"

      resources :tasks

      resources :microposts

      resources :users

      root :to => 'static_pages#home'
    end

我想要做的是链接到每个索引页面,就像这里的那样

<h1>Listing tasks</h1>

<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Status</th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>

  <tbody>
    <% @tasks.each do |task| %>
      <tr>
        <td><%= task.name %></td>
        <td><%= task.status %></td>
        <td><%= link_to 'Show', task %></td>
        <td><%= link_to 'Edit', edit_task_path(task) %></td>
        <td><%= link_to 'Destroy', task, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New Task', new_task_path %>

还有一个返回主页的链接。抱歉,这是一个时髦的问题吗?

【问题讨论】:

  • Marshall,将您的目标移到消息的第一部分可能会有所帮助,您的“我想要做什么...”隐藏在代码块之间,很容易错过。

标签: html ruby-on-rails ruby


【解决方案1】:

请查看http://guides.rubyonrails.org/routing.html - 这是一个很好的参考!根据链接目标应该是什么,您可能正在寻找:

<%= link_to 'User (view)', user_path(@user) %>
<%= link_to 'User (edit)', edit_user_path(@user) %>
<%= link_to 'User (index)', users_path %>

最好, 本。

【讨论】:

    【解决方案2】:

    在您看来,您希望为每个使用适当的路径助手。获取索引页面:

    <%= link_to 'users', users_path %>
    <%= link_to 'microposts', microposts_path %>
    

    【讨论】:

      【解决方案3】:

      在您的任务控制器中,在 index 方法中,将这三个变量设置为您想要的值,

      例如@user=current_user, @micropost = @user.micropost, etc.

      【讨论】:

        【解决方案4】:

        你可以试试。

        <%= link_to 'users', :controller =>"user" %>
        <%= link_to 'microposts', :controller =>"micropost" %>
        <%= link_to 'task', :controller => "task" %>
        

        link_to参考

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-08-30
          • 2015-06-04
          • 2017-09-08
          • 1970-01-01
          • 2021-12-05
          • 2016-05-04
          相关资源
          最近更新 更多