문제

Okay so I have a post index and would like to link it to my slide index, but for some reason i keep getting this error

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

when i link with this

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

If i use the same link above in my Posts Edit Viewer, it seems to work fine....any suggestions?

I would basically like to link to this....../posts/:id/slides from the 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

SLIDE MODEL

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 CONTROLLER

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

SLIDE CONTROLLER

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
도움이 되었습니까?

해결책

The post_slides_path is looking for an id as the parameter to match to the /posts/:id/slides route. The reason it works in your edit page is because your @post variable is finding the id of the Post object ( @post = Post.find(params[:id]) ). In your index action of the Post controllers, you have the @posts instance variable pointing to the search params and paginating and you do not have a @post instance variable defined.

In your block try

 post_slides_path(post.id)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top