show view不依赖于特定ID

时间:2017-01-16 21:53:40

标签: ruby-on-rails ruby view undefined

我实际上是在使用webApp来播放音乐。

我创建了一个专辑和一个曲目模型。

曲目属于专辑。专辑has_many曲目

我的节目视图遇到了麻烦。

我有一个索引来显示所有专辑和曲目。

<table>   <thead>
    <tr>
      <td>#</td>
      <td>Title</td>
      <td>released_at</td>
    </tr>   </thead>   <tbody>
    <% @albums.each do |album|%>
    <td><%= link_to "show", albums_path%></td>
    <tr>
      <td><%= album.id%></td>
      <td><%= album.title %></td>
      <td><%= sexy_date(album.released_at)%></td>
      <td><%= time_ago_in_words(album.released_at)%></td>
      <td><%= album.tracks_count%></td>
      <% album.tracks.each do |track|%>
      <td>
        <%= track.title%>
      </td>
    </tr>
    <% end %>
  <% end %>
</tbody>

我想点击我的link_to

的特定相册
<td><%= link_to "show", albums_path%></td>

但事实是这段代码使网址 localhost:3000 / albums 而不是我正在寻找的 localhost:3000 / album / 1

我知道我的链接不正确,但我无法找到代码。

这是我的Albums_controller

 class AlbumsController < ApplicationController   
 # before_action :authenticate_user!   # before_action :set_track, only: [:show, :edit, :update, :destroy] 



  def index
     @albums = Album.all
  end

   def show   end

   def create
     @album = Album.new(album_params)   end

   private
     def set_album
       @album = Album.find(params[:id])
     end

     def album_params
       params.require(:album).permit(:title)
     end   end

感谢您的帮助:)

1 个答案:

答案 0 :(得分:1)

您可以通过执行以下操作链接到特定相册:

<td><%= link_to "show", album_path(album)%></td>

以下是有关如何使用link_tohttp://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to

的一些信息