0

我的 chap_three_controller.rb 文件:

class ChapThreeController < ApplicationController
   def create
      @marker = Marker.new(params[:m])
      if @marker.save
         res={:success=>true,:content=>"<div><strong>found </strong>#{marker.found}
              </div><div><strong>left </strong>#{marker.left}</div>"}
      else
         res={:success=>false,:content=>"Could not save the marker"}
      end
      render :text=>res.to_json
   end
end

我的 routes.rb 文件

match '/map3', :to => 'chap_three#map'
match '/map3/create', :to => 'chap_three#:action'

我是否正确地将控制器中的创建功能与我的路线相匹配?因为它不工作..

这是我的 javascript 代码片段:

request.open('GET', 'create' + getVars, true);
 request.onreadystatechange = function() {
    if (request.readyState == 4) {
 //the request is complete
    var success=false;
    var content='Error contacting web service';
    try {
 //parse the result to JSON (simply by eval-ing it)
      res=eval( "(" + request.responseText + ")" );
      content=res.content;
      success=res.success;
    }catch (e){
       success=false;
     }

我不断收到的错误是“联系网络服务时出错”。这意味着我的 request.responseText 不起作用,并且我的控制器中的 create 方法没有做任何事情......任何帮助都会很棒

4

1 回答 1

0

你希望你的比赛是:

match '/map3/create' => 'chap_three#create'

#分别分隔控制器名称和动作名称,并且是:to不必要的,仅在定义root路由时使用。我建议阅读Rails 路由指南

于 2013-02-08T05:08:10.230 回答