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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/401241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:52:06  来源:igfitidea点击:

Adding an action to an existing controller (Ruby on Rails)

ruby-on-railsrubyrails-routing

提问by Mark

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

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

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.

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

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

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

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 ?

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

Below is my posts_controller.rb

下面是我的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.

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

采纳答案by zenazn

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

您的路由未设置为允许该路由。假设你使用的是默认的脚手架,把这个线之前map.resources :posts在配置/ routes.rb中线路:

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

The regex for :actionrestricts 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.

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

回答by David

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)

基本上,Rails 会自动为您的脚手架映射 URL。因此,当您创建 Posts 脚手架时,Rails 正在为其映射 URL 路由。一种这样的路由是查看单个帖子的 URL:/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.

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

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

# 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.

无论如何,希望有帮助。

回答by Nailson Landim

On Rails 4.x use:

在 Rails 4.x 上使用:

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

On Rails 3.x use:

在 Rails 3.x 上使用:

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

instead of

代替

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

it solved my issue.

它解决了我的问题。

回答by Amanuel

Try this if you are using rails 3.0.3

如果您使用的是 rails 3.0.3,请尝试此操作

in your route.rb

在你的 route.rb

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

this might help

这可能有帮助

回答by ghayes

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

我想说的是,即使在开发环境中,Rails 有时也会粘着路由缓存。

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

重新启动 Rails 服务器可能会有所帮助。收到此错误时,这对我有用的次数超过了我的计数。

回答by chernia

This works:

这有效:

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

回答by Thiago Diniz

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

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

map.resource :post

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

我添加了集合参数,所以它保持这种方式:

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