2015-08-29 212 views
-2

我已经使用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' 
+0

route.rb中有什么? – trh

回答

1

路由信息时,正确设置,那么下面的应该是这个问题:

new_user_album_path() 

请求路径嵌套资源,并且需要得到父母对象,这是你的用户:

new_user_album_path(user_id: current_user.id) 

我认为即使这个工程:

new_user_album_path(current_user) 

如果这不起作用, 您应检查线路都像你认为他们是从外壳调用

rake routes 

寻找在输出new_user_album_path

  • 它真的在那里吗?
  • 所有名称部分的单数和复数是否相同?
  • 它需要什么参数?
+0

(user_id:current_user.id) –

+0

这对我有效 –

+0

new.html.erb文件中存在另一个错误 –

0

其他的东西,可能是不正确的,但在你albums#index你定义的变量@albums,但你在你的链接,你使用@album

更新

<%= link_to("New Article, new_user_album_path(@album.users.id)

<%= link_to("New Article, new_user_album_path(@albums.users.id)

接收不同的错误。关于你的目标的更多解释会有所帮助。 :)

0

请更改为:

<%= link_to 'New Article', new_user_album_path(current_user) %> 
+0

未定义的方法'用户'为#

+0

它给出了上述错误 –

+0

是的,@albums是一个“关系”或专辑列表。该列表没有用户属性,只有奇异相册 – Meier