views:

703

answers:

3

Hi,

I have an action in my PostsController named 'tagged', which I want to return all posts tagged with whatever term.

In my routes.rb I have the following (at the top):

map.connect 'posts/tagged/:tag', { :controller => 'posts', :action => 'tagged', :tag => /[a-z\-]+/ }

Yet navigating to posts/tagged/yes returns a RecordNotFound error:

Couldn't find Post without an ID

In my tagged.html.erb file, I'll eventually be using the find_tagged_with method from acts_as_taggable_on_steroids, but for now I've put a simple Post.find(:all) to eliminate the possibility of error.

It seems like my map.connect is being overridden, and the same error occurs even if I comment the whole of the routes.rb file out except my new line.

+1  A: 

Ok, because you can comment out the default routes that means your problem is not in your routes at all. It's that your tagged action in the posts controller probably has something like this.

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

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @post }
    end
  end

Or perhaps if you spent a little more time it looks like this:

  def tagged
    @post = Post.find(params[:tagged])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @post }
    end
  end

Where as what you want is this:

  def tagged
    @post = Post.find(:all, :conditions => {:tagged => params[:tagged]})

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @post }
    end
  end

Anyway, you should be writing functional tests for this stuff and not testing in the browser.

rabble
A: 

Thanks very much for your answer. Point taken about the testing, it's high on my to-do list.

My tagged action looks like this:

def tagged
  @posts = Post.find(:all)

  respond_to do |format|
    format.html
    format.xml { render :xml => @posts }
  end
end

Now, I know that doing find(:all) won't achieve what I eventually want, but I'm still getting an

ActiveRecord::RecordNotFound in PostsController#tagged

error. I agree with you that the route is not the problem, running rake routes confirms this. But I can't see what else I've done wrong.

Any ideas?

joecorcoran
A: 

Why not add a RESTful route for the "tagged" action?

map.resources :posts, :member => { :tagged => :put }
Josh