加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails中的问题保存模型

发布时间:2020-12-17 03:08:14 所属栏目:百科 来源:网络整理
导读:我正在建立一个带评论的简单博客.有一个Post模型和一个Comment模型.除了创建新注释之外,两者之间的每次交互都很好.在尝试发布新评论时,我在Rails中遇到问题: wrong number of arguments (1 for 0) 以下是请求参数(来自堆栈跟踪): {"commit"="Post My Comme
我正在建立一个带评论的简单博客.有一个Post模型和一个Comment模型.除了创建新注释之外,两者之间的每次交互都很好.在尝试发布新评论时,我在Rails中遇到问题:

wrong number of arguments (1 for 0)

以下是请求参数(来自堆栈跟踪):

{"commit"=>"Post My Comment","post_id"=>"1","authenticity_token"=>"5a7b3ad3dda771d4ef5eb5850018649a8be2af11","comment"=>{"name"=>"bj","website"=>"asdf.com","comment"=>"asdf","email"=>"asdf@asdf.com"}}

并且完整的堆栈跟踪:

/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/callbacks.rb:296:in `notify'
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/callbacks.rb:296:in `callback'
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/callbacks.rb:263:in `valid?'
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/validations.rb:900:in `save_without_dirty'
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/dirty.rb:75:in `save_without_transactions'
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/transactions.rb:106:in `save'
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/abstract/database_statements.rb:66:in `transaction'
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/transactions.rb:79:in `transaction'
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/transactions.rb:98:in `transaction'
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/transactions.rb:106:in `save'
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/transactions.rb:118:in `rollback_active_record_state!'
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/transactions.rb:106:in `save'
app/controllers/comments_controller.rb:39:in `create'
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/base.rb:1162:in `send'
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/base.rb:1162:in `perform_action_without_filters'
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/filters.rb:580:in `call_filters'
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/filters.rb:573:in `perform_action_without_benchmark'
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/rescue.rb:201:in `perform_action_without_caching'
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/caching/sql_cache.rb:13:in `perform_action'
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/abstract/query_cache.rb:33:in `cache'
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/query_cache.rb:8:in `cache'
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/caching/sql_cache.rb:12:in `perform_action'
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/base.rb:529:in `send'
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/base.rb:529:in `process_without_filters'
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/filters.rb:569:in `process_without_session_management_support'
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/session_management.rb:130:in `process'
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/base.rb:389:in `process'
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel/rails.rb:76:in `process'
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel/rails.rb:74:in `synchronize'
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel/rails.rb:74:in `process'
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:159:in `process_client'
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:in `each'
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:in `process_client'
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `run'
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `initialize'
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `new'
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `run'
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `initialize'
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `new'
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `run'
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel/configurator.rb:282:in `run'
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel/configurator.rb:281:in `each'
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel/configurator.rb:281:in `run'
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/bin/mongrel_rails:128:in `run'
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel/command.rb:212:in `run'
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/bin/mongrel_rails:281
/Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:502:in `load'
/Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:502:in `load'
/Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:354:in `new_constants_in'
/Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:502:in `load'
/Library/Ruby/Gems/1.8/gems/rails-2.1.0/lib/commands/servers/mongrel.rb:64
/Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:509:in `require'
/Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:354:in `new_constants_in'
/Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:509:in `require'
/Library/Ruby/Gems/1.8/gems/rails-2.1.0/lib/commands/server.rb:39

我已将错误缩小到我在调用模型上调用的save方法:

/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/transactions.rb:106:in `save'
app/controllers/comments_controller.rb:39:in `create'

在我的部分中生成错误的表单(当我发布时)如下(form_row是我创建的一个简单的帮助器方法,用于创建表单行):

<% form_for [post,new_comment] do |f| %>
<%= form_row('Name',f.text_field(:name,:size => 30),'comment_name') %>
<%= form_row('Email',f.text_field(:email,:size => 30) + content_tag(:span,"*",:class => "required"),'comment_email') %>
<%= form_row('Website',f.text_field(:website,'comment_website') %>
<%= form_row('Comment',f.text_area(:comment,{ :rows => 10,:cols => 40 }),'comment_comment') %>
<%= form_row('','<span class="sm-text"><span class="required">*</span> Denotes fields that will not be displayed on your comment</span>') %>
<%= form_row('',submit_tag("Post My Comment")) %>
<% end -%>

我怀疑问题是我如何使用form_for,但我已经尝试了所有可能的组合和咒语,但仍然无法弄清楚这个.我的帖子和评论模型如下:

class Post < ActiveRecord::Base
  has_many :comments
  has_many :tags,:through => :posts_tags
  belongs_to :user

  validates_presence_of :user_id,:title,:body
  validates_numericality_of :user_id
end

class Comment < ActiveRecord::Base
  belongs_to :posts

  validates_presence_of :post_id,:name,:email,:comment
  validates_numericality_of :post_id
end

最后,执行保存的控制器代码:

def create
  begin
    @comment = Comment.new(params[:comment])
    @comment.name = 'Anonymous' if @comment.name.nil?
    if @comment.save # the error occurs here
      ...

我真的很难过.在此先感谢您的帮助.

解决方法

您可能已在模型表中定义了通知属性(列),与框架方法notify(…)冲突.使用其他列名称,例如notify_me.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读