0

我有以下代码向我的控制器的方法发出 ajax 请求以获取给定 url 的内容类型:

    $("#wiki_form_url").change(function () {
        $.ajax({
            type: "GET",
            url: "/wiki_forms/content",
            data: {
                input_url: $("#wiki_form_url").val()
            },
            dataType: "text"
        }).done(function (data) {
                    // `data` contains the content-type
                    alert('Success');
                    console.log(data);
//                    alert(data);
                }).fail(function () {
                    alert("failed AJAX call");
                });
    });

在我的 wiki_forms 控制器中有一个名为 content 的方法,在里面我正在做:

  def content

    req = open(params[:input_url])
    render :text => req.content_type
    puts  "type is : #{req.content_type}"

  end

在我的 route.rb 文件中,我有:

 match "/wiki_forms/content" => 'wiki_forms#content'

但是,当我尝试发出 ajax 请求时,我得到了错误。我的控制台如下所示:

Started GET "/wiki_forms/content?input_url=http%3A%2F%2Fwww.ofdp.org%2Fbenchmark_indices%2F25" for 127.0.0.1 at 2013-03-28 14:08:42 -0400
Processing by WikiFormsController#show as TEXT
  Parameters: {"input_url"=>"http://www.ofdp.org/benchmark_indices/25", "id"=>"content"}
  WikiForm Load (0.3ms)  SELECT "wiki_forms".* FROM "wiki_forms" WHERE "wiki_forms"."id" = ? LIMIT 1  [["id", "content"]]
Completed 500 Internal Server Error in 3ms

ActiveRecord::RecordNotFound (Couldn't find WikiForm with id=content):
  app/controllers/wiki_forms_controller.rb:23:in `show'

为什么在我的 ajax 调用中指定内容方法时,这里会调用 show 方法?如何使这项工作?请帮忙

编辑_1:

耙路线

        wiki_forms GET    /wiki_forms(.:format)          wiki_forms#index
                   POST   /wiki_forms(.:format)          wiki_forms#create
     new_wiki_form GET    /wiki_forms/new(.:format)      wiki_forms#new
    edit_wiki_form GET    /wiki_forms/:id/edit(.:format) wiki_forms#edit
         wiki_form GET    /wiki_forms/:id(.:format)      wiki_forms#show
                   PUT    /wiki_forms/:id(.:format)      wiki_forms#update
                   DELETE /wiki_forms/:id(.:format)      wiki_forms#destroy
              root        /                              wiki_forms#index
wiki_forms_content        /wiki_forms/content(.:format)  wiki_forms#content
4

1 回答 1

4

文件的顺序routes.rb很重要。

rake routes节目

        wiki_forms GET    /wiki_forms(.:format)          wiki_forms#index
                   POST   /wiki_forms(.:format)          wiki_forms#create
     new_wiki_form GET    /wiki_forms/new(.:format)      wiki_forms#new
    edit_wiki_form GET    /wiki_forms/:id/edit(.:format) wiki_forms#edit
         wiki_form GET    /wiki_forms/:id(.:format)      wiki_forms#show
                   PUT    /wiki_forms/:id(.:format)      wiki_forms#update
                   DELETE /wiki_forms/:id(.:format)      wiki_forms#destroy
              root        /                              wiki_forms#index
wiki_forms_content        /wiki_forms/content(.:format)  wiki_forms#content

所以

         wiki_form GET    /wiki_forms/:id(.:format)      wiki_forms#show

首先匹配,:id具有值字符串“content”

移动你的

match "/wiki_forms/content" => 'wiki_forms#content'

在更高的地方routes.rb

于 2013-03-28T18:51:01.417 回答