36

我是 Ruby on Rails 的新手,我已经完成了博客教程

我现在正在尝试向控制器添加一个额外的操作,称为“开始”。

def start
end

我添加了一个视图页面“app/views/posts/start.html.erb”,只包含简单的 html。

当我转到 /posts/start 时,出现以下错误。

ActiveRecord::RecordNotFound in PostsController#show 
Couldn't find Post with ID=start

我了解错误,正在执行 show 操作,并且 start 不是有效的 ID。为什么不执行启动操作,是否缺少 MVC 架构或配置的某些部分?

下面是我的posts_controller.rb

class PostsController < ApplicationController

  # GET /posts/start
  def start
  end

  # GET /posts
  # GET /posts.xml
  def index
    @posts = Post.find(:all)
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @posts }
    end
  end

  # GET /posts/1
  # GET /posts/1.xml
  def show
    @post = Post.find(params[:id])
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @post }
    end
  end

end

是的,我已经重新启动了服务器并使用 Mongrel 和 webrick 进行了尝试。

4

7 回答 7

45

The error you're making is actually a pretty common one.

Basically, Rails automatically maps URLs for your scaffolds. So when you created the Posts scaffolds, Rails is mapping the URL routes for it. One such route is the URL for viewing a single post: /posts/(post_id)

So, when you're entering the URL /posts/start Rails thinks you're saying "Hey, give me the post with ID = start. So Rails complains that the show method can't find a post with such ID.

One quick way to fix this is to make sure your config/routes.rb has the route for the start action before the scaffolding routes:

# Route for start action
map.connect '/posts/start', :controller => 'posts', :action => 'start'
# Default mapping of routes for the scaffold
map.resources :posts

Anyway, hope that helps.

于 2008-12-30T20:08:06.770 回答
33

On Rails 4.x use:

get '/posts/start', :controller => 'posts', :action => 'start'

On Rails 3.x use:

match '/posts/start', :controller => 'posts', :action => 'start'

instead of

map.connect '/posts/start', :controller => 'posts', :action => 'start'

it solved my issue.

于 2011-09-28T14:00:43.867 回答
26

Your routing isn't set up to allow that route. Assuming you're using the default scaffolding, put this line before the map.resources :posts line in config/routes.rb:

map.connect "posts/:action", :controller => 'posts', :action => /[a-z]+/i

The regex for :action restricts it to just a-z (to avoid catching things like /posts/1). It can be improved if you need underscores or numbers in your new actions.

于 2008-12-30T20:06:27.273 回答
8

Try this if you are using rails 3.0.3

in your route.rb

 resource :posts do
   collection do
     get 'start'
   end
 end

this might help

于 2010-12-29T07:21:40.033 回答
2

I want to say, sometimes Rails gets sticky with routes-caching, even in the development environment.

It may help to restart your Rails server. This has worked for me more times than I can count when receiving this error.

于 2011-08-09T22:27:13.320 回答
1

This works:

map.resource :post, :collection => { :my_action => :get}
于 2010-02-16T10:15:14.840 回答
0

I found the solution to my problem, in the routes.rb file where

map.resource :post

I added with collection argument, so it stayed this way:

map.resource :post, :collection => { :my_action => :get}
于 2009-07-22T03:09:55.257 回答