我已經使用devise gem進行身份驗證。在同一個項目中我使用了相冊模型。但我的new_user_album_path是給錯誤。我認爲有用戶ID問題。請幫助我。路徑導軌無法正常工作
這是index.html.erb文件
<%= link_to 'New Article', new_user_album_path() %>
<table>
<tr>
<th>Title</th>
<th>Description</th>
</tr>
<% @albums.each do |album| %>
<tr>
<td><%= album.title %></td>
<td><%= album.description %></td>
<td><%= link_to 'Show', user_album_path(album.user,album) %></td>
<td><%= link_to 'Edit', edit_user_album_path(album.user,album) %></td>
<td><%= link_to 'destroy', user_album_path(album.user,album),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
this is albums_controller file
class AlbumsController < ApplicationController
before_action :authenticate_user!
def index
@albums = current_user.albums.all
end
def show
@album = current_user.albums.find(params[:id])
end
def new
@album = current_user.albums.new
end
def edit
@album = current_user.albums.find(params[:id])
end
def create
@album = current_user.albums.build(album_params)
if @album.save
redirect_to action: 'index'
else
render 'new'
end
end
def update
@album = current_user.albums.find(params[:id])
if @album.update(album_params)
redirect_to action: 'show'
else
render 'edit'
end
end
def destroy
@album = current_user.albums.find(params[:id])
@album.destroy
redirect_to action: 'index'
end
private
def album_params
params.require(:album).permit(:title, :description, :user_id)
end
end
it is giving following errror.->
NoMethodError in Albums#index
undefined method `users' for nil:NilClass
Extracted source (around line #2):
<h1>Your Albums</h1>
<%= link_to 'New Article', new_user_album_path(@album.users.id) %>
<table>
<tr>
<th>Title</th>
<th>Description</th>
route.rb file
devise_for :users
# The priority is based upon order of creation: first created priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
resources :users do
resources :albums
end
root 'albums#index'
route.rb中有什麼? – trh