我有以下代码向我的控制器方法发出ajax请求以获取给定网址的内容类型:
$("#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'
为什么在这里调用show方法,因为我在ajax调用中指定了content方法?如何使这项工作?请帮忙
EDIT_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
答案 0 :(得分: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
中更高。