Ruby-on-rails 找不到控制器的操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6624968/
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-03 01:32:59  来源:igfitidea点击:

The action could not be found for controller

ruby-on-railsmodelcontrollerroutesinitialization

提问by overtone

Having gone through my code I have a separate problem from my original question and rather than writing a new question. I will leave the old part at the bottom of this and post the new problem here. I do this because they are closely related.

通过我的代码,我有一个与原始问题不同的问题,而不是写一个新问题。我将把旧部分留在本文底部,并在此处发布新问题。我这样做是因为它们密切相关。

New:

新的:

Im getting an error message saying

我收到一条错误消息说

Unknown action

The action 'response' could not be found for CrawlerController

I'll keep it simple but the code for model, controller and routes are below in the previous question.

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

A basic run down is response is a def within CrawlerController as is add_Request. The routes are matched as such:

一个基本的失败是响应是 CrawlerController 中的一个定义,就像 add_Request 一样。路由匹配如下:

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

Here is controller code as per user request:

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

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

采纳答案by Bohdan

please recheck code of controller as I can see it

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

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

so one end is missing an your responseaction is defined inside add_Request

所以一端缺少一个你的response动作是在里面定义的add_Request