无法找到控制器的操作

时间:2011-07-08 13:15:42

标签: ruby-on-rails model controller routes initialization

完成我的代码后,我的原始问题出现了另外一个问题,而不是写一个新问题。我将旧部分留在底部,并在此处发布新问题。我这样做是因为它们密切相关。

新:

我收到一条错误消息

Unknown action

The action 'response' could not be found for CrawlerController

我会保持简单,但模型,控制器和路线的代码在上一个问题中如下。

基本运行是响应是CrawlerController中的def,就像add_Request一样。 路线匹配如下:

  match  "/requests/new" => "crawler#add_Request"
  match 'requests/:id' => 'crawler#response'   

以下是根据用户请求的控制器代码:

class CrawlerController < ApplicationController
def add_Request
@request = Request.new(params[:request])

respond_to do |format|
  if @request.save
    format.html { redirect_to(@request, :notice => 'Request was successfully created.') }
    format.xml  { render :xml => @request, :status => :created, :location => @request }

  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @request.errors, :status => :unprocessable_entity }
  end
end
end

def response
  @request = Request.find(params[:id])
 respond_to do |format|
   format.html
   format.js { render :json => @request }
  end
end

def show
  @request = Request.find(params[:id])

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

1 个答案:

答案 0 :(得分:4)

请重新检查控制器的代码,因为我可以看到它

class CrawlerController < ApplicationController
  def add_Request
    @request = Request.new(params[:request])

    respond_to do |format|
      if @request.save
        format.html { redirect_to(@request, :notice => 'Request was successfully created.') }
        format.xml  { render :xml => @request, :status => :created, :location => @request }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @request.errors, :status => :unprocessable_entity }
      end
    end

    def response
      @request = Request.find(params[:id])
      respond_to do |format|
        format.json {render :@request.to_json}
    end
  end

所以在response

中定义了add_Request动作,结果遗漏了一端