1

i'm actually working on a webApp to stream music.

I created an Album and a track model.

A track belongs_to an Album. An album has_many tracks

I'm in trouble with my show view.

I'v got an index to show all the albums and the tracks that go in.

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

i would like to click on a specific album with my link_to

<td><%= link_to "show", albums_path%></td>

but the fact is that this code make the url localhost:3000/albums and not localhost:3000/album/1 that i'm looking for.

I understand that my link is not correct, but i can't find what to code instead.

Here is my 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

Thanks for helping :)

Che
  • 112
  • 9

1 Answers1

1

You will be able to link to a specific album by doing the following:

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

Here is some information on how to use link_to: http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to

Dawcars
  • 378
  • 2
  • 8
  • Oh thanks ! i'm gonna read it ! If i writte your code, rails tells me to writte albums_path(album) because he doesn't know album_path method. Then everything is working, the only curiosity, is that when i click on show. The url is "http://localhost:3000/albums.3" and not http://localhost:3000/albums/3" pretty strange ? – Che Jan 16 '17 at 22:06
  • How have you defined album in your routes file? – Dawcars Jan 16 '17 at 22:31
  • i'v got a root to: 'albums#index' and a "resource :albums" – Che Jan 16 '17 at 22:34
  • Try changing `resource :albums` to `resources :albums` and then `album_path(album)` should work – Dawcars Jan 16 '17 at 22:38
  • 1
    `resource` and `resources` define different routes. See the following link for more information: http://stackoverflow.com/questions/9194767/difference-between-resource-and-resources-methods – Dawcars Jan 16 '17 at 22:39
  • Thanks for the links and answers, you were true ! :D everything is ok now ! – Che Jan 18 '17 at 22:35