Rails 3 form_for Argument Error(1表示0)但它*必须*至少有一个参数,不是吗?

时间:2011-11-20 20:46:20

标签: ruby-on-rails-3 forms cancan

我似乎无法想出这个......

据我所知,你必须为form_for视图帮助器提供一个实例变量,但是当我这样做时会引发错误......

对于一个简单的脚手架form_for我在wrong number of arguments (1 for 0)动作上提供模型实例时会出现参数错误new

相关代码:

控制器

class AdminMessagesController < ApplicationController
  before_filter :authenticate_user!
  load_and_authorize_resource

  def new
    @admin_message = AdminMessage.new
    logger.debug instance_variables

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @admin_message }
    end
  end

Controller instance_variables

[:@_routes, :@_action_has_layout, :@_view_context_class, :@_headers, :@_status, :@_request, :@_response, :@_env, :@_prefixes, :@_lookup_context, :@_action_name, :@_response_body, :@_config, :@_params, :@current_user, :@current_ability, :@admin_message, :@_authorized]

模型

class AdminMessage < ActiveRecord::Base
  validates_presence_of :subject, :body
end

角色(CanCan)

class Ability
  include CanCan::Ability

  def initialize(user)
    # Define abilities for the passed in user here. For example:

    user ||= User.new # guest user (not logged in)
    if user.admin?
      can :manage, :all
    elsif user
      can :manage, [Profile, BackgroundImage], :user_id => user.id
      can :manage, Page, :profile => { :user_id => user.id }
      can :read, [Profile]
    else
      can :read, [Profile]
    end

新视图

- @title = "New Message"
= link_to 'Back', admin_messages_path, class: 'button'
%hr
%h1= @title

= render 'form'

表格视图

= form_for @admin_message  do |f|
  -if @admin_message.errors.any?
    #error_explanation
      %h2= "#{pluralize(@admin_message.errors.count, "error")} prohibited this admin_message from being saved:"
      %ul
        - @admin_message.errors.full_messages.each do |msg|
          %li= msg

  .field
    = f.label :subject
    = f.text_field :subject
  .field
    = f.label :body
    = f.text_area :body
  .field
    = f.label :send
    = f.check_box :send
  .actions
    = f.submit 'Save'

路线

resources :admin_messages

堆栈跟踪

ArgumentError in Admin_messages#new

Showing /example/app/views/admin_messages/_form.html.haml where line #4 raised:

wrong number of arguments (1 for 0)
Extracted source (around line #4):

1: -# - puts self.public_methods
2: -# - puts self.instance_variables
3: 
4: = form_for @admin_message  do |f|
5:   -if @admin_message.errors.any?
6:     #error_explanation
7:       %h2= "#{pluralize(@admin_message.errors.count, "error")} prohibited this admin_message from being saved:"

完整堆栈跟踪

activerecord (3.1.2) lib/active_record/attribute_methods/read.rb:89:in `_send'
activerecord (3.1.2) lib/active_record/attribute_methods/primary_key.rb:8:in `to_key'
actionpack (3.1.2) lib/action_controller/record_identifier.rb:76:in `record_key_for_dom_id'
actionpack (3.1.2) lib/action_controller/record_identifier.rb:57:in `dom_id'
actionpack (3.1.2) lib/action_view/helpers/form_helper.rb:388:in `apply_form_for_options!'
actionpack (3.1.2) lib/action_view/helpers/form_helper.rb:365:in `form_for'
haml (3.1.3) lib/haml/helpers/action_view_mods.rb:182:in `form_for_with_haml'
haml (3.1.3) lib/haml/helpers/xss_mods.rb:132:in `form_for_with_haml_xss'
app/views/admin_messages/_form.html.haml:4:in `_app_views_admin_messages__form_html_haml__1654301332507974170_70145208768920'
actionpack (3.1.2) lib/action_view/template.rb:171:in `block in render'
activesupport (3.1.2) lib/active_support/notifications.rb:55:in `instrument'
actionpack (3.1.2) lib/action_view/template.rb:169:in `render'
actionpack (3.1.2) lib/action_view/renderer/partial_renderer.rb:256:in `render_partial'
actionpack (3.1.2) lib/action_view/renderer/partial_renderer.rb:228:in `block (2 levels) in render'
actionpack (3.1.2) lib/action_view/renderer/abstract_renderer.rb:33:in `block in instrument'
activesupport (3.1.2) lib/active_support/notifications.rb:53:in `block in instrument'
activesupport (3.1.2) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.1.2) lib/active_support/notifications.rb:53:in `instrument'
actionpack (3.1.2) lib/action_view/renderer/abstract_renderer.rb:33:in `instrument'
actionpack (3.1.2) lib/action_view/renderer/partial_renderer.rb:227:in `block in render'
actionpack (3.1.2) lib/action_view/renderer/abstract_renderer.rb:22:in `wrap_formats'
actionpack (3.1.2) lib/action_view/renderer/partial_renderer.rb:219:in `render'
actionpack (3.1.2) lib/action_view/renderer/renderer.rb:41:in `render_partial'
actionpack (3.1.2) lib/action_view/helpers/rendering_helper.rb:27:in `render'
haml (3.1.3) lib/haml/helpers/action_view_mods.rb:11:in `block in render_with_haml'
haml (3.1.3) lib/haml/helpers.rb:90:in `non_haml'
haml (3.1.3) lib/haml/helpers/action_view_mods.rb:11:in `render_with_haml'
app/views/admin_messages/new.html.haml:6:in `_app_views_admin_messages_new_html_haml__1382734216418550338_70145179834340'
actionpack (3.1.2) lib/action_view/template.rb:171:in `block in render'
activesupport (3.1.2) lib/active_support/notifications.rb:55:in `instrument'
actionpack (3.1.2) lib/action_view/template.rb:169:in `render'
actionpack (3.1.2) lib/action_view/renderer/template_renderer.rb:40:in `block (2 levels) in render_template'
actionpack (3.1.2) lib/action_view/renderer/abstract_renderer.rb:33:in `block in instrument'
activesupport (3.1.2) lib/active_support/notifications.rb:53:in `block in instrument'
activesupport (3.1.2) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.1.2) lib/active_support/notifications.rb:53:in `instrument'
actionpack (3.1.2) lib/action_view/renderer/abstract_renderer.rb:33:in `instrument'
actionpack (3.1.2) lib/action_view/renderer/template_renderer.rb:39:in `block in render_template'
actionpack (3.1.2) lib/action_view/renderer/template_renderer.rb:47:in `render_with_layout'
actionpack (3.1.2) lib/action_view/renderer/template_renderer.rb:38:in `render_template'
actionpack (3.1.2) lib/action_view/renderer/template_renderer.rb:12:in `block in render'
actionpack (3.1.2) lib/action_view/renderer/abstract_renderer.rb:22:in `wrap_formats'
actionpack (3.1.2) lib/action_view/renderer/template_renderer.rb:9:in `render'
actionpack (3.1.2) lib/action_view/renderer/renderer.rb:36:in `render_template'
actionpack (3.1.2) lib/action_view/renderer/renderer.rb:17:in `render'
actionpack (3.1.2) lib/abstract_controller/rendering.rb:120:in `_render_template'
actionpack (3.1.2) lib/action_controller/metal/streaming.rb:250:in `_render_template'
actionpack (3.1.2) lib/abstract_controller/rendering.rb:114:in `render_to_body'
actionpack (3.1.2) lib/action_controller/metal/renderers.rb:30:in `render_to_body'
actionpack (3.1.2) lib/action_controller/metal/compatibility.rb:43:in `render_to_body'
actionpack (3.1.2) lib/abstract_controller/rendering.rb:99:in `render'
actionpack (3.1.2) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (3.1.2) lib/action_controller/metal/instrumentation.rb:40:in `block (2 levels) in render'
activesupport (3.1.2) lib/active_support/core_ext/benchmark.rb:5:in `block in ms'
/Users/victorstan/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/benchmark.rb:310:in `realtime'
activesupport (3.1.2) lib/active_support/core_ext/benchmark.rb:5:in `ms'
actionpack (3.1.2) lib/action_controller/metal/instrumentation.rb:40:in `block in render'
actionpack (3.1.2) lib/action_controller/metal/instrumentation.rb:78:in `cleanup_view_runtime'
activerecord (3.1.2) lib/active_record/railties/controller_runtime.rb:24:in `cleanup_view_runtime'
actionpack (3.1.2) lib/action_controller/metal/instrumentation.rb:39:in `render'
actionpack (3.1.2) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (3.1.2) lib/action_controller/metal/mime_responds.rb:268:in `block in retrieve_response_from_mimes'
actionpack (3.1.2) lib/action_controller/metal/mime_responds.rb:195:in `call'
actionpack (3.1.2) lib/action_controller/metal/mime_responds.rb:195:in `respond_to'
app/controllers/admin_messages_controller.rb:33:in `new'
actionpack (3.1.2) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.1.2) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.1.2) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.1.2) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.1.2) lib/active_support/callbacks.rb:434:in `_run__1801240210143518790__process_action__2938188811796736454__callbacks'
activesupport (3.1.2) lib/active_support/callbacks.rb:386:in `_run_process_action_callbacks'
activesupport (3.1.2) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.1.2) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.1.2) lib/action_controller/metal/rescue.rb:17:in `process_action'
actionpack (3.1.2) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.1.2) lib/active_support/notifications.rb:53:in `block in instrument'
activesupport (3.1.2) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.1.2) lib/active_support/notifications.rb:53:in `instrument'
actionpack (3.1.2) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.1.2) lib/action_controller/metal/params_wrapper.rb:201:in `process_action'
activerecord (3.1.2) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.1.2) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.1.2) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.1.2) lib/action_controller/metal.rb:193:in `dispatch'
actionpack (3.1.2) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.1.2) lib/action_controller/metal.rb:236:in `block in action'
actionpack (3.1.2) lib/action_dispatch/routing/route_set.rb:65:in `call'
actionpack (3.1.2) lib/action_dispatch/routing/route_set.rb:65:in `dispatch'
actionpack (3.1.2) lib/action_dispatch/routing/route_set.rb:29:in `call'
rack-mount (0.8.3) lib/rack/mount/route_set.rb:152:in `block in call'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:96:in `block in recognize'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:82:in `optimized_each'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:95:in `recognize'
rack-mount (0.8.3) lib/rack/mount/route_set.rb:141:in `call'
actionpack (3.1.2) lib/action_dispatch/routing/route_set.rb:532:in `call'
sass (3.1.10) lib/sass/plugin/rack.rb:54:in `call'
warden (1.1.0) lib/warden/manager.rb:35:in `block in call'
warden (1.1.0) lib/warden/manager.rb:34:in `catch'
warden (1.1.0) lib/warden/manager.rb:34:in `call'
actionpack (3.1.2) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.3.5) lib/rack/etag.rb:23:in `call'
rack (1.3.5) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.1.2) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.1.2) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.1.2) lib/action_dispatch/middleware/flash.rb:247:in `call'
rack (1.3.5) lib/rack/session/abstract/id.rb:195:in `context'
rack (1.3.5) lib/rack/session/abstract/id.rb:190:in `call'
actionpack (3.1.2) lib/action_dispatch/middleware/cookies.rb:331:in `call'
activerecord (3.1.2) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.1.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:477:in `call'
actionpack (3.1.2) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (3.1.2) lib/active_support/callbacks.rb:392:in `_run_call_callbacks'
activesupport (3.1.2) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.1.2) lib/action_dispatch/middleware/callbacks.rb:28:in `call'
actionpack (3.1.2) lib/action_dispatch/middleware/reloader.rb:68:in `call'
rack (1.3.5) lib/rack/sendfile.rb:101:in `call'
actionpack (3.1.2) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
actionpack (3.1.2) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call'
railties (3.1.2) lib/rails/rack/logger.rb:13:in `call'
rack (1.3.5) lib/rack/methodoverride.rb:24:in `call'
rack (1.3.5) lib/rack/runtime.rb:17:in `call'
activesupport (3.1.2) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.3.5) lib/rack/lock.rb:15:in `call'
actionpack (3.1.2) lib/action_dispatch/middleware/static.rb:53:in `call'
railties (3.1.2) lib/rails/engine.rb:456:in `call'
rack (1.3.5) lib/rack/content_length.rb:14:in `call'
railties (3.1.2) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.3.5) lib/rack/handler/webrick.rb:59:in `service'
/Users/victorstan/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
/Users/victorstan/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
/Users/victorstan/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'

更新

如果我尝试检查控制器#new操作中的@admin_message var,则错误消息会更改为指示控制器而不是视图中的错误...

ArgumentError in AdminMessagesController#new

wrong number of arguments (1 for 0)

如果我将puts @admin_message.inspect添加到控制器操作new

,就会发生这种情况

1 个答案:

答案 0 :(得分:1)

故事的道德:不要将send用作表格列名称!