将操作添加到现有控制器 (Ruby on Rails)

发布于 2024-07-10 05:16:37 字数 1106 浏览 6 评论 0原文

我是 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

我了解该错误,正在执行显示操作,并且启动不是有效的 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。

I am new to Ruby on Rails, I have completed the Blog Tutorial.

I am now trying to add an additional action to the controller, called 'start'.

def start
end

I have added a view page "app/views/posts/start.html.erb" containing nothing but simple html.

When I go to /posts/start i get the following error.

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

I understand the error, the show action is being executed and start is not a valid ID. Why doesn't the start action get executed, is there some part of the MVC architecture or configuration I am missing ?

Below is my 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

Yes I have restarted the server and tried it with Mongrel and webrick.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

凉城已无爱 2024-07-17 05:16:38

您所犯的错误实际上是一个很常见的错误。

基本上,Rails 会自动映射您的脚手架的 URL。 因此,当您创建 Posts 脚手架时,Rails 正在为其映射 URL 路由。 其中一个路由是用于查看单个帖子的 URL: /posts/(post_id)

因此,当您输入 URL /posts/start 时,Rails 认为您是在说“嘿,给我 ID = start 的帖子。所以Rails 抱怨 show 方法找不到具有此类 ID 的帖子。

解决此问题的一种快速方法是确保您的 config/routes.rb 在脚手架路由之前具有启动操作的路由:

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

无论如何,希望有所帮助。

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.

只等公子 2024-07-17 05:16:38

On Rails 4.x use:

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

On Rails 3.x use:

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

而不是

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

它解决了我的问题。

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.

花之痕靓丽 2024-07-17 05:16:38

您的路由未设置为允许该路由。 假设您使用默认的脚手架,请将此行放在 config/routes.rb 中的 map.resources :posts 行之前:

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

:action< 的正则表达式/code> 将其限制为 az (以避免捕获 /posts/1 等内容)。 如果您在新操作中需要下划线或数字,则可以对其进行改进。

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.

撞了怀 2024-07-17 05:16:38

使用rails 3.0.3,请尝试此操作,

如果您在route.rb中

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

这可能会有所帮助

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

只等公子 2024-07-17 05:16:38

我想说,有时 Rails 会因为路由缓存而变得粘滞,即使在开发环境中也是如此。

重新启动 Rails 服务器可能会有所帮助。 当我收到此错误时,这对我来说已经起作用了很多次。

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.

梦里梦着梦中梦 2024-07-17 05:16:38

这有效:

map.resource :post, :collection => { :my_action => :get}

This works:

map.resource :post, :collection => { :my_action => :get}
嘿嘿嘿 2024-07-17 05:16:38

我在routes.rb文件中找到了问题的解决方案,

map.resource :post

我在其中添加了集合参数,所以它保持这样:

map.resource :post, :collection => { :my_action => :get}

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}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文