如何在rails上链接页面?

时间:2021-02-28 20:45:31

I have three resources for my demo_app that I made. I made a home page but I wanted to put links of my home page to link to my three resources. I put out the code

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

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

on my home page and the links show up on the page but they don't work

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

routes.rb file

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

      resources :tasks

      resources :microposts

      resources :users

      root :to => 'static_pages#home'
    end

what I want to do is link to the index page of each of these like the one here

我想要做的是链接到这些中的每一个的索引页面

<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 %>

and also have a link back to the home page. sorry is this a funky question?

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

4 个解决方案

#1


4  

Please look into http://guides.rubyonrails.org/routing.html - it is a great reference! Depending on what the link target is supposed to be you are probably looking for:

请查看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 %>

Best, Ben.

#2


1  

In your view you want to use an appropriate path helper for each. To get the index pages:

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

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

#3


0  

In your tasks controller, in the index method, set those three variables to the values you want,

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

e.g. @user=current_user, @micropost = @user.micropost, etc.

例如@ user = current_user,@ microstost = @ user.micropost等

#4


0  

You can try it.

你可以尝试一下。

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

reference by link_to

link_to引用

#1


4  

Please look into http://guides.rubyonrails.org/routing.html - it is a great reference! Depending on what the link target is supposed to be you are probably looking for:

请查看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 %>

Best, Ben.

#2


1  

In your view you want to use an appropriate path helper for each. To get the index pages:

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

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

#3


0  

In your tasks controller, in the index method, set those three variables to the values you want,

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

e.g. @user=current_user, @micropost = @user.micropost, etc.

例如@ user = current_user,@ microstost = @ user.micropost等

#4


0  

You can try it.

你可以尝试一下。

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

reference by link_to

link_to引用