Rails 3没有路由匹配(嵌套资源?)

时间:2012-01-09 05:04:05

标签: ruby-on-rails-3 routes

我在rails中有一对多关联,其中用户has_many:专辑和专辑belongs_to:user。一切正常,但索引页面。每当我尝试进入索引页面时,我都会得到No routes matches错误。有什么我做错了吗?除了显示所有专辑的索引页面外,我的所有页面都有效。

* 这是错误(带有所有信息的数组,我截断了大部分信息)

No route matches {:action=>"show", :controller=>"albums", :user_id=>[#<User id: 3}

的routes.rb

  resources :users do
    resources :albums
  end

albums_controller.rb

class AlbumsController < ApplicationController

  def index
    @albums = Album.all
    @user = User.all
  end

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

  def new
    @album = Album.new
    @user = User.new
  end

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

  def create
    @album = current_user.albums.create(params[:album])

     if @album.save
       redirect_to user_albums_path, :notice => 'Album was successfully created.' 
     else
       render :action => "new"
     end
  end

  def update
    @album = Album.find(params[:id])

      if @album.update_attributes(params[:album])
        redirect_to user_album_path(@album), :notice => 'Album was successfully updated.' 
      else
      render :action => "new"
    end
  end

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

专辑的表单

<%= form_for ([:user, @album]) , :html => {:multipart => true} do |f| %>

相册index.html.erb

<% for albums in @albums %>
 <tr>
   <td><%= albums.title %></td>
   <td><%= albums.description %></td>
   <td><%= albums.user_id %></td>
   <td><%= link_to 'Show', user_album_path(@user, albums) %></td>
   <td><%= link_to 'Edit', edit_user_album_path(@user, albums) %></td>
   <td><%= link_to 'Destroy', user_album_path(@user, albums), confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>


1 个答案:

答案 0 :(得分:1)

是的,我认为我没有在指定专辑之前给用户打电话,在大多数情况下这些专辑是@user = User.find(params[:user_id])