嵌套资源链接从索引不起作用

时间:2012-08-01 20:56:50

标签: ruby-on-rails-3 routes nested-routes

好的,所以我有一个帖子索引,想把它链接到我的幻灯片索引,但由于某些原因我不断收到此错误

No route matches {:controller=>"slides"}

当我链接到

<%= link_to 'Show Slides', post_slides_path(@post) %>

如果我在帖子编辑查看器中使用上面的相同链接,它似乎工作正常....任何建议?

我基本上想要链接到这个...... / posts /:id / slide from my table in posts

的routes.rb

resources :posts do
    resources :slides
end

POST MODEL

class Post < ActiveRecord::Base
  attr_accessible :text, :title

  has_many :slides, :dependent => :destroy

  def self.search(search)
    if search
      where('Title Like ?' , "%#{search}%")
    else
      scoped
    end
  end

幻灯片型号

class Slide < ActiveRecord::Base
  belongs_to :post

POST INDEX VIEW

<table id="posts" class="table table-striped table-bordered">
  <thead>
   <tr>
     <th>Title</th>
     <th>Description</th>
   </tr>
 </thead>
 <tbody>
   <% @posts.each do |post| %>
     <tr>
       <td><%= link_to 'Show Slides', post_slides_path(@post) %>
       <td><%= link_to 'Edit', :action => :edit, :id => post.id %></td>
       <td><%= link_to 'Destroy', { :action => :destroy, :id => post.id }, :method => :delete, :confirm => 'Are you sure?' %></td>
     </tr> 
   <% end %>
 <tbody>
<% end %>
</table>

POST控制器

class PostsController < ApplicationController

  def new
    @post = Post.new
  end

  def show
    @post = Post.find(params[:id])
  end

  def index
    @posts = Post.search(params[:search]).paginate(:per_page => 10, :page =>    params[:page])
  end

  def edit
    @post = Post.find(params[:id])
  end

幻灯片控制器

class SlidesController < ApplicationController

  def index
    @post = Post.find(params[:post_id])
    @slides = @post.slides.all
  end

  def show
    @post = Post.find(params[:post_id])
    @slide = @post.slides.find(params[:id])
  end

  def new
    @post = Post.find(params[:post_id])
    @slide = Slide.new
  end

  def edit
    @post = Post.find(params[:post_id])
    @slide = Slide.find(params[:id])
  end

1 个答案:

答案 0 :(得分:1)

post_slides_path正在寻找一个id作为匹配/ posts /:id / slides路由的参数。它在编辑页面中工作的原因是因为你的@post变量正在查找Post对象的id(@ post = Post.find(params [:id]))。在Post控制器的索引操作中,您有@posts实例变量指向搜索参数和分页,并且您没有定义@post实例变量。

在您的区块中尝试

 post_slides_path(post.id)