简单的Rails AJAX Call不会像JS那样处理

时间:2012-03-12 12:22:54

标签: ruby-on-rails ajax

为什么在服务器端将带有HTML data-remote =“true”的表单作为HTML进行处理?

我的index.html.erb文件中只有一个简单的表过滤器:

 <%= form_tag('by_subject', :remote => true,  :method => "get" ) do  %>
<%= select_tag( "subject_id", options_for_select(Subject.all.collect {|p| [ p.name, p.id ] })) %>
    <%= submit_tag "Search" %>
 <% end %>

数据远程HTML似乎在那里:

<form accept-charset="UTF-8" action="by_subject" data-remote="true" method="get"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /></div>
<select id="subject_id" name="subject_id">
        <option value="1">Art</option>
        <option value="2">Business &amp; Tech</option>
        <option value="15">undefined</option>
        <option value="16">Music</option></select>
     <input name="commit" type="submit" value="Search" />
 </form>

但是我的控制器动作

def by_subject
@courses = Course.where(:subject_id => params[:subject_id])
respond_to do |format|
  format.html { raise "It rendered HTML again."  }
  format.js   { raise "Ah, it wants to render Javascript."  }
  format.json { render json: @courses }
end

收益率

It rendered HTML again.

...

Request

Parameters:

{"utf8"=>"✓",
 "subject_id"=>"16",
 "commit"=>"Search"}

每一次。我尝试过各种各样的情感。

谢谢!

和env转储:

GATEWAY_INTERFACE: "CGI/1.2"
HTTP_ACCEPT: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
HTTP_ACCEPT_ENCODING: "gzip, deflate"
HTTP_ACCEPT_LANGUAGE: "en-us,en;q=0.5"
REMOTE_ADDR: "127.0.0.1"
SERVER_NAME: "localhost"
SERVER_PROTOCOL: "HTTP/1.1"

好的,我有几页Rack @env。不确定需要多少钱。整件事情都在这里gist

#<Rack::Request:0x4396260 @env={"SERVER_SOFTWARE"=>"thin 1.3.1 codename Triple Espresso", 
"SERVER_NAME"=>"localhost", "rack.input"=>#<StringIO:0x459f720>, "rack.version"=>[1, 0], "rack.errors"=>#<IO:<STDERR>>, 
"rack.multithread"=>false, "rack.multiprocess"=>false, "rack.run_once"=>false, 
"REQUEST_METHOD"=>"GET", 
"REQUEST_PATH"=>"/courses/by_subject", 
"PATH_INFO"=>"/courses/by_subject", 
"QUERY_STRING"=>"utf8=%E2%9C%93&subject_id=16&commit=Search", 
"REQUEST_URI"=>"/courses/by_subject?utf8=%E2%9C%93&subject_id=16&commit=Search", 
"HTTP_VERSION"=>"HTTP/1.1", 
"HTTP_HOST"=>"localhost:3000", 
"HTTP_USER_AGENT"=>"Mozilla/5.0 (Windows NT 6.0; rv:10.0.2) Gecko/20100101 Firefox/10.0.2", 
"HTTP_ACCEPT"=>"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 
"HTTP_ACCEPT_LANGUAGE"=>"en-us,en;q=0.5", 
"HTTP_ACCEPT_ENCODING"=>"gzip, deflate", 
"HTTP_CONNECTION"=>"keep-alive", 
"HTTP_REFERER"=>"http://localhost:3000/courses", 
"HTTP_COOKIE"=>"__utma=111872281.1794585154.1323696638.1326994528.1326997977.9; __utmz=111872281.1323696638.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); _creditflex_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJWZhNDZhZmMyZDU4OTczMGM0NDNmMDBiMjhiMzc3YzNlBjsAVEkiEF9jc3JmX3Rva2VuBjsARkkiMTFhaFNlRUcyTmErSWZDQWtQdmF5WEg2L3RRNG00Q2tBRVZBUDJCdlZ0RWM9BjsARg%3D%3D--ec4ddf8986b88c6aae5429305fbf735527770d95", 
"GATEWAY_INTERFACE"=>"CGI/1.2", "SERVER_PORT"=>"3000", 
"SERVER_PROTOCOL"=>"HTTP/1.1", "rack.url_scheme"=>"http", 
"SCRIPT_NAME"=>"", "REMOTE_ADDR"=>"127.0.0.1", 
"async.callback"=>#<Method: Thin::Connection#post_process>, "async.close"=>#<EventMachine::DefaultDeferrable:0x45ab6f0>, 
"ORIGINAL_FULLPATH"=>"/courses/by_subject?utf8=%E2%9C%93&subject_id=16&commit=Search", 
"action_dispatch.routes"=>#<ActionDispatch::Routing::RouteSet:0x4893330 @named_routes=#<ActionDispatch::Routing::RouteSet::NamedRouteCollection:0x4893318 
@routes={:ckeditor=>#<Journey::Route:0x40b8490 @name="ckeditor", @app=Ckeditor::Engine, @path=#<Journey::Path::Pattern:0x40b8f58 @anchored=false, @spec=/ckeditor, @requirements={}, @separators="/.?", @names=[], @optional_names=nil, @required_names=nil, @re=/\A\/ckeditor/>, @verb=//, @ip=//, @constraints={}, @defaults={}, @required_defaults=nil, @required_parts=nil, @parts=[], @decorated_ast=nil, @precedence=1>, 
:response=>#<Journey::Route:0x4a74d10 @name="response", @app=#<ActionDispatch::Routing::RouteSet::Dispatcher:0x45fa8d8 @defaults={:action=>"new", :controller=>"responses"}, @glob_param=nil, @controllers={}>, @path=#<Journey::Path::Pattern:0x45fa098 @anchored=true, @spec=/responses/:id(.:format),

....

:by_subject_courses=>#<Journey::Route:0x5535e40 @name="by_subject_courses", @app=#<ActionDispatch::Routing::RouteSet::Dispatcher:0x5537580 @defaults={:action=>"by_subject", :controller=>"courses"}, @glob_param=nil, @controllers={"courses"=>#<ActiveSupport::Dependencies::ClassCache:0x3cc3fd8 @store={"CoursesController"=>CoursesController, "Subject"=>Subject(id: integer, name: string, created_at: datetime, updated_at: datetime, course_id: integer)}>}>, 
@path=#<Journey::Path::Pattern:0x5537388 @anchored=true, @spec=/courses/by_subject(.:format), @requirements={}, @separators="/.?", @names=["format"], @optional_names=nil, @required_names=nil, @re=/\A\/courses\/by_subject(?:\.([^\/.?]+))?\Z/>, @verb=/^GET$/, @ip=//, @constraints={:request_method=>/^GET$/}, @defaults={:action=>"by_subject", :controller=>"courses"}, @required_defaults=nil, @required_parts=nil, @parts=[:format], @decorated_ast=/courses/by_subject(.:format), @precedence=116>, 
:courses=>#<Journey::Route:0x553a028 @name="courses", @app=#<ActionDispatch::Routing::RouteSet::Dispatcher:0x553b270 @defaults={:action=>"index", :controller=>"courses"}, @glob_param=nil, @controllers={"courses"=>#<ActiveSupport::Dependencies::ClassCache:0x3cc3fd8 @store={"CoursesController"=>CoursesController, "Subject"=>Subject(id: integer, name: string, created_at: datetime, updated_at: datetime, course_id: integer)}>}>, 
@path=#<Journey::Path::Pattern:0x553b078 @anchored=true, @spec=/courses(.:format), @requirements={}, @separators="/.?", @names=["format"], @optional_names=nil, @required_names=nil, @re=/\A\/courses(?:\.([^\/.?]+))?\Z/>, @verb=/^GET$/, @ip=//, @constraints={:request_method=>/^GET$/}, @defaults={:action=>"index", :controller=>"courses"}, @required_defaults=nil, @required_parts=nil, @parts=[:format], @decorated_ast=/courses(.:format), @precedence=117>, ...

...

1 个答案:

答案 0 :(得分:3)

我的猜测是jquery(默认JS框架)$ .ajax函数的dataType未设置或设置不正确。如果未设置dataType,jQuery docs会说:

  

如果没有指定,jQuery将尝试根据响应的MIME类型推断它(XML MIME类型将产生XML,在1.4 JSON中将产生一个JavaScript对象,在1.4脚本中将执行脚本,以及任何东西else将作为字符串返回。)

我认为这是jquery-ujs(轨道附带的不显眼的javascript)相关部分:https://github.com/rails/jquery-ujs/blob/6186a854e9cd291045da3c98c31fb32ff2cf0aa6/src/rails.js#L97

line 106上有:

dataType = element.data('type') || ($.ajaxSettings && $.ajaxSettings.dataType)

我不知道你的js或jquery-ujs中的其他地方是否有任何一个。我还强调了上面第97行,因为它说:

// Default ajax function, may be overridden with custom function in $.rails.ajax
ajax: function(options) {
  return $.ajax(options);
},

所以我不确定哪种方式最好尝试设置dataType;它可能取决于您是否希望所有ajax请求都是特定的数据类型。