0

我有一条路线:

match "/ajax/municipios" => "ajax#municipios", as: :ajax_municipios

我有javascript(coffeescript):

$ ->
  $("#porto_uf_id").live("change", (event) -> 
    $.ajax({
      dataType: "json",
      type: "get",
      url: "ajax/municipios",
      data: { uf_id: $(this).val() },
      success: (data) ->
        alert(data)
    })

这行得通...但是当我将类型更改为发布时,出现错误...

$ ->
  $("#porto_uf_id").live("change", (event) -> 
    $.ajax({
      dataType: "json",
      type: "post",
      url: "ajax/municipios",
      data: { uf_id: $(this).val() },
      success: (data) ->
        alert(data)
    })

错误:POST http://localhost:3000/ajax/municipios 500 (Internal Server Error)

在控制台中:

NoMethodError - undefined method `model_name' for ActiveRecord::Relation:Class:
  activemodel (3.2.13) lib/active_model/naming.rb:163:in `model_name_from_record_or_class'
  activemodel (3.2.13) lib/active_model/naming.rb:131:in `singular_route_key'
  actionpack (3.2.13) lib/action_dispatch/routing/polymorphic_routes.rb:186:in `build_named_route_call'
  actionpack (3.2.13) lib/action_dispatch/routing/polymorphic_routes.rb:120:in `polymorphic_url'
  actionpack (3.2.13) lib/action_dispatch/routing/url_for.rb:150:in `url_for'
  actionpack (3.2.13) lib/action_controller/metal/rendering.rb:60:in `_process_options'
  actionpack (3.2.13) lib/action_controller/metal/streaming.rb:208:in `_process_options'
  actionpack (3.2.13) lib/action_controller/metal/renderers.rb:34:in `block in _handle_render_options'
  /home/vmlellis/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/set.rb:232:in `each'
...

这是怎么回事?

日期:

部分rake routes命令:

portos          GET    /portos(.:format)              portos#index
                POST   /portos(.:format)              portos#create
new_porto       GET    /portos/new(.:format)          portos#new                     
ajax_municipios POST   /ajax/municipios(.:format)     ajax#municipios

控制器:

class AjaxController < ApplicationController
    respond_to :json

    def municipios
        uf_id = params["uf_id"]

        municipios = Municipio.order(:nome).select([:id,:nome])
        municipios = municipios.where(uf_id: uf_id) if uf_id

        respond_with municipios
    end
end

回溯:

Started POST "/ajax/municipios" for 127.0.0.1 at 2013-06-19 12:13:52 -0300
Processing by AjaxController#municipios as JSON
  Parameters: {"uf_id"=>"27"}
  User Load (0.7ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
Completed 500 Internal Server Error in 8ms

NoMethodError - undefined method `model_name' for ActiveRecord::Relation:Class:
  activemodel (3.2.13) lib/active_model/naming.rb:163:in `model_name_from_record_or_class'
  activemodel (3.2.13) lib/active_model/naming.rb:131:in `singular_route_key'
  actionpack (3.2.13) lib/action_dispatch/routing/polymorphic_routes.rb:186:in `build_named_route_call'
  actionpack (3.2.13) lib/action_dispatch/routing/polymorphic_routes.rb:120:in `polymorphic_url'
  actionpack (3.2.13) lib/action_dispatch/routing/url_for.rb:150:in `url_for'
  actionpack (3.2.13) lib/action_controller/metal/rendering.rb:60:in `_process_options'
  actionpack (3.2.13) lib/action_controller/metal/streaming.rb:208:in `_process_options'
  actionpack (3.2.13) lib/action_controller/metal/renderers.rb:34:in `block in _handle_render_options'
  /home/vmlellis/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/set.rb:232:in `each'
  actionpack (3.2.13) lib/action_controller/metal/renderers.rb:32:in `_handle_render_options'
  actionpack (3.2.13) lib/action_controller/metal/renderers.rb:28:in `render_to_body'
  actionpack (3.2.13) lib/action_controller/metal/compatibility.rb:50:in `render_to_body'
  actionpack (3.2.13) lib/abstract_controller/rendering.rb:88:in `render'
  actionpack (3.2.13) lib/action_controller/metal/rendering.rb:16:in `render'
  actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:40:in `block (2 levels) in render'
  activesupport (3.2.13) lib/active_support/core_ext/benchmark.rb:5:in `block in ms'
  /home/vmlellis/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/benchmark.rb:296:in `realtime'
  activesupport (3.2.13) lib/active_support/core_ext/benchmark.rb:5:in `ms'
  actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:40:in `block in render'
  actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:83:in `cleanup_view_runtime'
  activerecord (3.2.13) lib/active_record/railties/controller_runtime.rb:24:in `cleanup_view_runtime'
  actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:39:in `render'
  remotipart (1.0.5) lib/remotipart/render_overrides.rb:14:in `render_with_remotipart'
  actionpack (3.2.13) lib/action_controller/metal/responder.rb:254:in `display'
  actionpack (3.2.13) lib/action_controller/metal/responder.rb:204:in `api_behavior'
  actionpack (3.2.13) lib/action_controller/metal/responder.rb:181:in `rescue in to_format'
  actionpack (3.2.13) lib/action_controller/metal/responder.rb:175:in `to_format'
  actionpack (3.2.13) lib/action_controller/metal/responder.rb:153:in `respond'
  actionpack (3.2.13) lib/action_controller/metal/responder.rb:146:in `call'
  actionpack (3.2.13) lib/action_controller/metal/mime_responds.rb:239:in `respond_with'
  app/controllers/ajax_controller.rb:10:in `municipios'
  actionpack (3.2.13) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
  actionpack (3.2.13) lib/abstract_controller/base.rb:167:in `process_action'
  actionpack (3.2.13) lib/action_controller/metal/rendering.rb:10:in `process_action'
  actionpack (3.2.13) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
  activesupport (3.2.13) lib/active_support/callbacks.rb:502:in `_run__4170375937101831970__process_action__2304024013048582180__callbacks'
  activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'
  activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
  activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (3.2.13) lib/abstract_controller/callbacks.rb:17:in `process_action'
  actionpack (3.2.13) lib/action_controller/metal/rescue.rb:29:in `process_action'
  actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
  activesupport (3.2.13) lib/active_support/notifications.rb:123:in `block in instrument'
  activesupport (3.2.13) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (3.2.13) lib/active_support/notifications.rb:123:in `instrument'
  actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
  actionpack (3.2.13) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
  activerecord (3.2.13) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  actionpack (3.2.13) lib/abstract_controller/base.rb:121:in `process'
  actionpack (3.2.13) lib/abstract_controller/rendering.rb:45:in `process'
  actionpack (3.2.13) lib/action_controller/metal.rb:203:in `dispatch'
  actionpack (3.2.13) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
  actionpack (3.2.13) lib/action_controller/metal.rb:246:in `block in action'
  actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
  actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:36:in `call'
  journey (1.0.4) lib/journey/router.rb:68:in `block in call'
  journey (1.0.4) lib/journey/router.rb:56:in `call'
  actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:612:in `call'
  rack-pjax (0.7.0) lib/rack/pjax.rb:12:in `call'
  bullet (4.6.0) lib/bullet/rack.rb:13:in `call'
  client_side_validations (3.2.5) lib/client_side_validations/middleware.rb:21:in `call'
  warden (1.2.1) lib/warden/manager.rb:35:in `block in call'
  warden (1.2.1) lib/warden/manager.rb:34:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
  rack (1.4.5) lib/rack/etag.rb:23:in `call'
  rack (1.4.5) lib/rack/conditionalget.rb:35:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/head.rb:14:in `call'
  remotipart (1.0.5) lib/remotipart/middleware.rb:30:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/flash.rb:242:in `call'
  rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'
  rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/cookies.rb:341:in `call'
  activerecord (3.2.13) lib/active_record/query_cache.rb:64:in `call'
  activerecord (3.2.13) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
  activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `_run__4407205321283737826__call__699633464861693726__callbacks'
  activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'
  activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
  activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/reloader.rb:65:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
  better_errors (0.9.0) lib/better_errors/middleware.rb:84:in `protected_app_call'
  better_errors (0.9.0) lib/better_errors/middleware.rb:79:in `better_errors_call'
  better_errors (0.9.0) lib/better_errors/middleware.rb:56:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.13) lib/rails/rack/logger.rb:32:in `call_app'
  railties (3.2.13) lib/rails/rack/logger.rb:16:in `block in call'
  activesupport (3.2.13) lib/active_support/tagged_logging.rb:22:in `tagged'
  railties (3.2.13) lib/rails/rack/logger.rb:16:in `call'
  quiet_assets (1.0.2) lib/quiet_assets.rb:18:in `call_with_quiet_assets'
  actionpack (3.2.13) lib/action_dispatch/middleware/request_id.rb:22:in `call'
  rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
  rack (1.4.5) lib/rack/runtime.rb:17:in `call'
  activesupport (3.2.13) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/static.rb:63:in `call'
  railties (3.2.13) lib/rails/engine.rb:479:in `call'
  railties (3.2.13) lib/rails/application.rb:223:in `call'
  rack (1.4.5) lib/rack/content_length.rb:14:in `call'
  railties (3.2.13) lib/rails/rack/log_tailer.rb:17:in `call'
  thin (1.5.1) lib/thin/connection.rb:81:in `block in pre_process'
  thin (1.5.1) lib/thin/connection.rb:79:in `pre_process'
  thin (1.5.1) lib/thin/connection.rb:54:in `process'
  faye-websocket (0.6.1) lib/faye/adapters/thin.rb:40:in `process'
  thin (1.5.1) lib/thin/connection.rb:39:in `receive_data'
  faye-websocket (0.6.1) lib/faye/adapters/thin.rb:44:in `receive_data'
  eventmachine (1.0.3) lib/eventmachine.rb:187:in `run'
  thin (1.5.1) lib/thin/backends/base.rb:63:in `start'
  thin (1.5.1) lib/thin/server.rb:159:in `start'
  rack (1.4.5) lib/rack/handler/thin.rb:13:in `run'
  rack (1.4.5) lib/rack/server.rb:268:in `start'
  railties (3.2.13) lib/rails/commands/server.rb:70:in `start'
  railties (3.2.13) lib/rails/commands.rb:55:in `block in <top (required)>'
  railties (3.2.13) lib/rails/commands.rb:50:in `<top (required)>'
  script/rails:6:in `<main>'
4

2 回答 2

1

用葡萄牙语命名它们。当您使用正确的屈折变化时,它没有任何区别!

于 2013-06-20T18:19:06.087 回答
0

尝试:

render json: municipios

我觉得有些问题respond_with

于 2013-06-20T18:17:53.020 回答