Rails 4.2.1:无法创建记录

时间:2017-10-01 02:31:20

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

今天我尝试像往常一样创建一个新模型我先创建了我的控制器:

rails g controller Newsletters

然后我创建了我的模型:

rails g model Newsletter

然后我更新了我的迁移:

class CreateNewsletters < ActiveRecord::Migration
  def change
    create_table :newsletters do |t|
      t.string :subject
      t.text :message
      t.string :recipient
      t.boolean :send, default: false

      t.timestamps null: false
    end
  end
end

最后我进行了迁移:

rake db:migrate

我设置了我的控制器:

class NewslettersController < ApplicationController
  def index
    @newsletters = Newsletter.all
  end

  def show
    @newsletter = Newsletter.find_by(id: params[:id])
  end

  def new
    @newletter = Newsletter.new
  end

  def create
    @newletter = Newsletter.new(newsletter_params)
    if @newsletter.save
      flash[:success] = "Newsletter saved"
      redirect_to newletter_path
    else
      render 'new'
    end
  end

  def edit
  end

  def update
  end

  def destroy
    newsletter = Newsletter.find_by(id: params[:id]).destroy
    flash[:success] = "Successfully destroyed"
  end


  private
    def newsletter_params
      params.require(:newsletter).permit(:subject, :message, :recipient, :send)
    end
end

现在问题就在于此。当我尝试在控制台中创建记录时,我收到此错误:

irb(main):001:0> newsletter = Newsletter.create(subject: "test", message: "test test", recipient: "all")
ArgumentError: wrong number of arguments (given 2, expected 0)
    from /Users/alexander/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/activerecord-4.2.1/lib/active_record/attribute_methods.rb:48:in `__temp__3756e646'
    from /Users/alexander/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/activerecord-4.2.1/lib/active_record/attribute_methods.rb:379:in `clone_attribute_value'
    from /Users/alexander/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/activerecord-4.2.1/lib/active_record/attribute_methods/dirty.rb:122:in `old_attribute_value'
    from /Users/alexander/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/activerecord-4.2.1/lib/active_record/attribute_methods/dirty.rb:94:in `write_attribute'
    from /Users/alexander/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/activerecord-4.2.1/lib/active_record/attribute_methods.rb:50:in `__temp__375726a6563647'
    from /Users/alexander/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/activerecord-4.2.1/lib/active_record/attribute_assignment.rb:54:in `public_send'
    from /Users/alexander/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/activerecord-4.2.1/lib/active_record/attribute_assignment.rb:54:in `_assign_attribute'
    from /Users/alexander/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/activerecord-4.2.1/lib/active_record/attribute_assignment.rb:41:in `block in assign_attributes'
    from /Users/alexander/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/activerecord-4.2.1/lib/active_record/attribute_assignment.rb:35:in `each'
    from /Users/alexander/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/activerecord-4.2.1/lib/active_record/attribute_assignment.rb:35:in `assign_attributes'
    from /Users/alexander/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/activerecord-4.2.1/lib/active_record/core.rb:559:in `init_attributes'
    from /Users/alexander/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/activerecord-4.2.1/lib/active_record/core.rb:281:in `initialize'
    from /Users/alexander/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/activerecord-4.2.1/lib/active_record/inheritance.rb:61:in `new'
    from /Users/alexander/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/activerecord-4.2.1/lib/active_record/inheritance.rb:61:in `new'
    from /Users/alexander/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/activerecord-4.2.1/lib/active_record/persistence.rb:33:in `create'
    from (irb):1
    from /Users/alexander/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/railties-4.2.1/lib/rails/commands/console.rb:110:in `start'
    from /Users/alexander/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/railties-4.2.1/lib/rails/commands/console.rb:9:in `start'
    from /Users/alexander/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/railties-4.2.1/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /Users/alexander/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/railties-4.2.1/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /Users/alexander/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/railties-4.2.1/lib/rails/commands.rb:17:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'irb(main):002:0> 

我到目前为止尝试了什么:

  1. 我把所有内容都卷了回去,删除了所有生成的文件,并再次手工完成。
  2. 我再次安装捆绑包以防万一过时
  3. 我真的不知道错误是什么,如果有人可以帮助我,我会非常感激。

    PS:我已经在github上看到它可能是“rb-fsevent”宝石的问题,但是我的日志没有显示同样的事情:

    https://github.com/guard/listen/issues/431

    更新:

    我的简报型号

    class Newsletter < ActiveRecord::Base
    end
    

    PS:我正在运行ruby 2.3.0

1 个答案:

答案 0 :(得分:3)

send是一个Ruby方法,当你有一个同名的属性时会导致问题。

只需将该列重命名为其他内容 - 例如email_sendwas_send,您就可以了。