Rails jQuery POST - ArgumentError - 错误的参数个数(1表示0)?

时间:2014-06-24 06:52:41

标签: ruby-on-rails ruby-on-rails-4

我正在使用rails 4.1。

我已经设置了自定义控制器。我已使用以下命令配置路由:

resources :chats

我正在尝试使用jQuery向控制器发送帖子请求。这是代码:

$.post('/chats/', { session: { advisor_id: $(this).attr('data-id') } }, function (data) {
    eval(data);
});

编辑:这是我控制器的代码 -

class ChatsController < ApplicationController

  def create
    #create the new session
    @session = Session.create(:advisor_id => session_params[:advisor_id], :session_type => @session.call!, :client_id => current_user.id)

  end

  private
    def session_params
     params[:session].permit(:advisor_id, :client_id, :session_type)
    end
end

编辑:这是我模型的代码 -

class Session < ActiveRecord::Base
  enum session_type: [ :chat, :call ]
  belongs_to :client, :class_name => "User", :foreign_key => :client_id
  belongs_to :advisor, :class_name => "User", :foreign_key => :advisor_id
  has_many :instant_messages

  def self.obj_name(id)
    o = Session.find(id)
    "#{o.client.full_name} and #{o.advisor.full_name}"
  end
end

然而它会抛出一个错误:

  

ArgumentError - 参数数量错误(0表示0):     activesupport(4.1.1)lib / active_support / callbacks.rb:80:in run_callbacks' actionpack (4.1.1) lib/abstract_controller/callbacks.rb:19:in process_action&#39;     actionpack(4.1.1)lib / action_controller / metal / rescue.rb:29:在process_action&#39;中的process_action' actionpack (4.1.1) lib/action_controller/metal/instrumentation.rb:31:in块中     activesupport(4.1.1)lib / active_support / notifications.rb:159:in block in instrument' activesupport (4.1.1) lib/active_support/notifications/instrumenter.rb:20:in instrument&#39;     activesupport(4.1.1)lib / active_support / notifications.rb:159:在instrument' actionpack (4.1.1) lib/action_controller/metal/instrumentation.rb:30:in process_action&#39;     actionpack(4.1.1)lib / action_controller / metal / params_wrapper.rb:250:在process_action' activerecord (4.1.1) lib/active_record/railties/controller_runtime.rb:18:in process_action&#39;     actionpack(4.1.1)lib / abstract_controller / base.rb:136:在process' actionview (4.1.1) lib/action_view/rendering.rb:30:in进程&#39;     actionpack(4.1.1)lib / action_controller / metal.rb:195:在dispatch' actionpack (4.1.1) lib/action_controller/metal/rack_delegation.rb:13:in发送&#39;     actionpack(4.1.1)lib / action_controller / metal.rb:231:在block in action' actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:80:in发送&#39;     actionpack(4.1.1)lib / action_dispatch / routing / route_set.rb:48:in call' actionpack (4.1.1) lib/action_dispatch/journey/router.rb:71:in阻止通话&#39;     actionpack(4.1.1)lib / action_dispatch / journey / router.rb:59:在call' actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:676:in电话&#39;     rack-pjax(0.7.0)lib / rack / pjax.rb:12:in call' warden (1.2.3) lib/warden/manager.rb:35:in块中的呼叫&#39;     warden(1.2.3)lib / warden / manager.rb:34:in call' rack (1.5.2) lib/rack/etag.rb:23:in来电&#39;     rack(1.5.2)lib / rack / conditionalget.rb:35:in call' rack (1.5.2) lib/rack/head.rb:11:in call&#39;     remotipart(1.2.1)lib / remotipart / middleware.rb:27:in call' actionpack (4.1.1) lib/action_dispatch/middleware/params_parser.rb:27:in call&#39;     actionpack(4.1.1)lib / action_dispatch / middleware / flash.rb:254:在call' rack (1.5.2) lib/rack/session/abstract/id.rb:225:in上下文&#39;     rack(1.5.2)lib / rack / session / abstract / id.rb:220:in call' actionpack (4.1.1) lib/action_dispatch/middleware/cookies.rb:560:in call&#39;     activerecord(4.1.1)lib / active_record / query_cache.rb:36:in call' activerecord (4.1.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in call&#39;     activerecord(4.1.1)lib / active_record / migration.rb:380:在call' actionpack (4.1.1) lib/action_dispatch/middleware/callbacks.rb:29:in阻止呼叫&#39;     activesupport(4.1.1)lib / active_support / callbacks.rb:82:in run_callbacks' actionpack (4.1.1) lib/action_dispatch/middleware/callbacks.rb:27:in call&#39;     actionpack(4.1.1)lib / action_dispatch / middleware / reloader.rb:73:in call' actionpack (4.1.1) lib/action_dispatch/middleware/remote_ip.rb:76:in call&#39;     better_errors(1.1.0)lib / better_errors / middleware.rb:84:in protected_app_call' better_errors (1.1.0) lib/better_errors/middleware.rb:79:in better_errors_call&#39;     better_errors(1.1.0)lib / better_errors / middleware.rb:56:in call' actionpack (4.1.1) lib/action_dispatch/middleware/debug_exceptions.rb:17:in call&#39;     actionpack(4.1.1)lib / action_dispatch / middleware / show_exceptions.rb:30:in call' railties (4.1.1) lib/rails/rack/logger.rb:38:in call_app&#39;     railties(4.1.1)lib / rails / rack / logger.rb:20:block in call' activesupport (4.1.1) lib/active_support/tagged_logging.rb:68:in块中标记的&#39;     activesupport(4.1.1)lib / active_support / tagged_logging.rb:26:in tagged' activesupport (4.1.1) lib/active_support/tagged_logging.rb:68:in已标记&#39;     railties(4.1.1)lib / rails / rack / logger.rb:20:in call' quiet_assets (1.0.2) lib/quiet_assets.rb:18:in call_with_quiet_assets&#39;     actionpack(4.1.1)lib / action_dispatch / middleware / request_id.rb:21:in call' rack (1.5.2) lib/rack/methodoverride.rb:21:in call&#39;     rack(1.5.2)lib / rack / runtime.rb:17:in call' activesupport (4.1.1) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in call&#39;     rack(1.5.2)lib / rack / lock.rb:17:in call' actionpack (4.1.1) lib/action_dispatch/middleware/static.rb:64:in call&#39;     rack(1.5.2)lib / rack / sendfile.rb:112:call' railties (4.1.1) lib/rails/engine.rb:514:in来电&#39;     railties(4.1.1)lib / rails / application.rb:144:in call' rack (1.5.2) lib/rack/content_length.rb:14:in来电&#39;     thin(1.6.2)lib / thin / connection.rb:86:in block in pre_process' thin (1.6.2) lib/thin/connection.rb:84:in pre_process&#39;     thin(1.6.2)lib / thin / connection.rb:53:in process' thin (1.6.2) lib/thin/connection.rb:39:in receive_data&#39;     eventmachine(1.0.3)lib / eventmachine.rb:187:在run' thin (1.6.2) lib/thin/backends/base.rb:73:in开始&#39;     thin(1.6.2)lib / thin / server.rb:162:in start' rack (1.5.2) lib/rack/handler/thin.rb:16:in run&#39;     rack(1.5.2)lib / rack / server.rb:264:in start' railties (4.1.1) lib/rails/commands/server.rb:69:in start&#39;     railties(4.1.1)lib / rails / commands / commands_tasks.rb:81:in block in server' railties (4.1.1) lib/rails/commands/commands_tasks.rb:76:in server&#39;     railties(4.1.1)lib / rails / commands / commands_tasks.rb:40:in run_command!' railties (4.1.1) lib/rails/commands.rb:17:in&#39;     bin / rails:8:在<top (required)>' ruby-debug-ide (0.4.23.beta1) lib/ruby-debug-ide.rb:86:in debug_program&#39;     ruby-debug-ide(0.4.23.beta1)bin / rdebug-ide:110:<top (required)>' -e:1:in&#39;

为什么会抛出此错误?如何让它接受请求?

1 个答案:

答案 0 :(得分:8)

我设法解决了这个问题。问题是我在控制器中有一个叫做“发送”的动作。我现在知道“发送”是ruby中用来调用其他方法的方法。

以下是有关ruby“send”方法的更多信息。 What does send() do in Ruby?