【问题标题】:Rails Update Action fails with rails4, mongoid. Create okRails 更新操作因 rails4、mongoid 而失败。创建好
【发布时间】:2015-03-26 06:31:56
【问题描述】:

我有一个带有 rails4 和 mongoid 的简单 Rails 应用程序。我可以像魅力一样创建新的数据集。但我就是无法更新现有数据集。

有人有这个问题吗?这是怎么回事,我做错了什么?

刚刚使用 rails 4、ruby 2 和 mongoid 从头开始​​创建,全部来自他们的 git repos:

rails new mongotest --skip-active-record

我生成了一个脚手架:

rails g scaffold things name description

我的模型现在看起来像这样:

class Thing
  include Mongoid::Document
  field :name, type: String
  field :description, type: String
end

这样的控制器:

class ThingsController < ApplicationController
  before_action :set_thing, only: [:show, :edit, :update, :destroy]

  # GET /things
  # GET /things.json
  def index
    @things = Thing.all
  end

  # GET /things/1
  # GET /things/1.json
  def show
  end

  # GET /things/new
  def new
    @thing = Thing.new
  end

  # GET /things/1/edit
  def edit
  end

  # POST /things
  # POST /things.json
  def create
    @thing = Thing.new(thing_params)

    respond_to do |format|
      if @thing.save
        format.html { redirect_to @thing, notice: 'Thing was successfully created.' }
        format.json { render action: 'show', status: :created, location: @thing }
      else
        format.html { render action: 'new' }
        format.json { render json: @thing.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /things/1
  # PATCH/PUT /things/1.json
  def update
    respond_to do |format|
      if @thing.update(thing_params)
        format.html { redirect_to @thing, notice: 'Thing was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @thing.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /things/1
  # DELETE /things/1.json
  def destroy
    @thing.destroy
    respond_to do |format|
      format.html { redirect_to things_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_thing
      @thing = Thing.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def thing_params
      params.require(:thing).permit(:name, :description)
    end
end

我的宝石文件:

source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails',     git: 'https://github.com/rails/rails.git'
gem 'arel',      git: 'https://github.com/rails/arel.git'
gem 'activerecord-deprecated_finders', git: 'https://github.com/rails/activerecord-deprecated_finders.git'

gem 'thin'
gem 'mongoid', git: 'https://github.com/mongoid/mongoid.git'



# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 4.0.0.beta1'
  gem 'coffee-rails', '~> 4.0.0.beta1'

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  # gem 'therubyracer', platforms: :ruby

  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'

# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.0.1'

这是来自尝试更新数据集时的日志:

Started PATCH "/things/51669078e05658cf22000001" for 127.0.0.1 at 2013-04-11 12:29:25 +0200
Processing by ThingsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"lfELkffFSf9gk04TnHnzG9cCrOe3XrsjK4fEZ7Rt7qQ=", "thing"=>{"name"=>"qqqq", "description"=>"qqqqq"}, "commit"=>"Update Thing", "id"=>"51669078e05658cf22000001"}
  MOPED: 127.0.0.1:27017 QUERY        database=mongotest_development collection=things selector={"_id"=>"51669078e05658cf22000001"} flags=[:slave_ok] limit=0 skip=0 batch_size=nil fields=nil (0.5772ms)
Redirected to http://localhost:3000/things/51669078e05658cf22000001
Completed 302 Found in 6ms

如果有人有任何提示或链接...非常感谢帮助。

更新 1:我确实在 rails 3 环境中尝试过,并且效果很好……完全符合预期。

更新 2:记录器输出带有“Mongoid.logger.level = Logger::DEBUG”和“Moped.logger.level = Logger::DEBUG”:

Started PATCH "/things/5166c5ece05658f08a000001" for 127.0.0.1 at 2013-04-11 16:20:56 +0200
Processing by ThingsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"lfELkffFSf9gk04TnHnzG9cCrOe3XrsjK4fEZ7Rt7qQ=", "thing"=>{"name"=>"EditTest", "description"=>"123"}, "commit"=>"Update Thing", "id"=>"5166c5ece05658f08a000001"}
D, [2013-04-11T16:20:56.450464 #37961] DEBUG -- :   MOPED: 127.0.0.1:27017 QUERY        database=mongotest_development collection=things selector={"_id"=>"5166c5ece05658f08a000001"} flags=[:slave_ok] limit=0 skip=0 batch_size=nil fields=nil (0.7041ms)
Redirected to http://localhost:3000/things/5166c5ece05658f08a000001
Completed 302 Found in 6ms

更新 3:我用于上次日志输出的 mongoid.yml:

development:
  # Configure available database sessions. (required)
  sessions:
    # Defines the default session. (required)
    default:
      # Defines the name of the default database that Mongoid can connect to.
      # (required).
      database: mongotest_development
      # Provides the hosts the default session can connect to. Must be an array
      # of host:port pairs. (required)
      hosts:
        - localhost:27017
      options:
        # Change whether the session persists in safe mode by default.
        # (default: false)
        safe: true

我的示例应用位于https://github.com/jlxq0/mongotest

更新 4:更多研究表明,这 / 可能 / 是类似的问题,如 Rails 4.0.0.beta 1 and Mongoid

如果没有人有答案,我也会很高兴有人分享一个指向工作 rails4 / Mongo 示例源的链接,这样我就可以自己找出差异。

更新 5:在 Rails 控制台中,更新工作正常。

【问题讨论】:

  • 在开发中使用安全模式在您的 mongoid 配置文件中查看 MongoDB 上的错误并回发您得到的错误。
  • 我按照Mongoid Docs 上的说明配置了日志记录输出(评论太长,将在下一条评论中发布)下来什么都不告诉我。

标签: ruby-on-rails ruby mongoid crud updates


【解决方案1】:

这是因为update 不接受属性,它只接受选项,例如@thing.update(validate: false)

为了让您的代码正常工作,您可以执行以下操作:

if @thing.update_attributes(thing_params)
  #...
end

或者:

@thing.attributes = thing_params
if @thing.save
  #...
end

或者:

@thing.attributes = thing_params
if @thing.update
  #...
end

【讨论】:

  • 完美答案,绝对解开我所有的谜团!非常感谢,modetojoy!
【解决方案2】:

MongoDB 有一种叫做安全模式的东西,默认情况下它是关闭的,这意味着无论你告诉它执行什么,它都会为它返回 OK。如果您将安全模式设置为 on,如果您执行 mongo 标准非法的查询,则会引发异常。

在开发过程中,您通常会这样做,

development:
  sessions:
    default:
      hosts:
        - localhost:27017
      database: mongoid
      username: user
      password: password
      options:
        safe: true

之后您可以将您的异常发布给我们,我们可以尝试解决它!

【讨论】:

  • 伙计,谢谢你的提示。我试过了,但老实说,我真的看不出有什么不寻常的地方。我将尝试在 GitHub 上分享整个解决方案。
  • 也可以尝试使用save!而不是save,查看异常情况
  • 我的控制器中只有 save 用于创建操作。创建工作正常。对于更新操作,它是@thing.update(thing_params)。我认为,没有update! 这样的东西。我已将我的完整来源添加到 this Github Repo
【解决方案3】:

如果您正在使用嵌入式属性(无论是动态创建的 Hash,还是明确定义的关系),使用 upsert 而不是 update 可能会有所帮助。

如果条目存在,Upsert 会导致字段被重写,如果不存在则创建。

person = Person.new(
  first_name: "Heinrich",
  last_name: "Heine"
)
person.upsert

示例来自:http://mongoid.org/en/mongoid/docs/persistence.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-10
    • 2014-06-20
    • 2023-04-02
    • 2013-09-17
    • 2012-09-04
    • 2022-01-01
    相关资源
    最近更新 更多