使用 ORM mangomapper 生成的 Rails 代码会抛出 NoMethodError (undefined method `each' for "4d2aeaea4403baa84a000005":String)

发布于 2024-10-11 09:02:28 字数 2981 浏览 2 评论 0 原文

我对 Rails 和 MongoDB 绝对是陌生的。我一直在遵循一本好书中的教程,并使用一个简单的 Twitter 副本创建了我的第一个 Rails 应用程序。一切都很顺利。

但作为我学习过程的一部分,我想使用 MongoDB 而不是默认的 SGBD 构建相同的应用程序。

因此,我配置了 mongo 并安装了 mongo_mapper gem。按照本教程,所有内容均已正确配置: http://www .mongodb.org/display/DOCS/Rails+3+-+入门+开始。然后我费了一番功夫才让 Rails generated 能够正常工作,而又不会抛出 --orm not specified 错误。为了解决这个问题,我必须添加 Rails3-generators gem 并将其添加到 Gemfile 中。

当这一切都完成后,一切都顺利进行。我能够成功启动 Rails 服务器。

感谢生成,我添加了一个用户控制器。该页面工作正常,甚至列出了我之前添加的用户:

alt text

但是所有其他操作,显示,编辑,删除等不起作用(创建作品,但随后显示并出现错误):

alt text

这是所有不同的操作几乎都会出现相同的错误。

我立刻注意到的一个区别是,对于非 MongoDB 数据库,用户的 id 是从 1 开始的,等等。但是对于 MongoDB,它看起来像是一个随机生成的 id,要复杂得多,而且不是int 类型:4d2ae91d4403baa84a000002

我认为这可能会造成问题,因为所有操作都使用 id 作为参数......但我不知道如何解决这个问题。我查看了 ruby​​ 生成的代码,它对我来说看起来不错(与默认数据库生成的代码非常相似)。

任何帮助将不胜感激!如果不解决使用 mongodb 的简单生成代码,我不知道如何继续我的项目。

谢谢,

亚历

克斯附: 请注意我根本没有编写任何这段代码。一切都已生成,这就是为什么我希望从一开始就开始工作...

正如这里所要求的是 users_controllers 的代码:

class UsersController < ApplicationController
  # GET /users
  # GET /users.xml
  def index
    @users = User.all

respond_to do |format|
  format.html # index.html.erb
  format.xml  { render :xml => @users }
    end
  end

  # GET /users/1
  # GET /users/1.xml
  def show
@user = User.first(params[:id])

respond_to do |format|
  format.html # show.html.erb
  format.xml  { render :xml => @user }
end
  end

  # GET /users/new
  # GET /users/new.xml
  def new
@user = User.new

respond_to do |format|
  format.html # new.html.erb
  format.xml  { render :xml => @user }
end
  end

# GET /users/1/edit
  def edit
@user = User.first(params[:id])
  end

  # POST /users
  # POST /users.xml
  def create
    @user = User.new(params[:user])

respond_to do |format|
  if @user.save
    format.html { redirect_to(@user, :notice => 'User was successfully created.') }
    format.xml  { render :xml => @user, :status => :created, :location => @user }
  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
  end
end
end

  # PUT /users/1
  # PUT /users/1.xml
  def update
@user = User.first(params[:id])

respond_to do |format|
  if @user.update(params[:user])
    format.html { redirect_to(@user, :notice => 'User was successfully updated.') }
    format.xml  { head :ok }
  else
    format.html { render :action => "edit" }
    format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
  end
end
  end

  # DELETE /users/1
  # DELETE /users/1.xml
  def destroy
@user = User.first(params[:id])
@user.destroy

respond_to do |format|
  format.html { redirect_to(users_url) }
  format.xml  { head :ok }
end
  end
end

I am absolutely totally news to Rails and to MongoDB. I have been following tutorials from a good book and create my first Rails app with a light Twitter copy. Everything went fine and smooth.

But as part of my learning process I wanted to build the same app using MongoDB rather than the default SGBD.

I therefore configured mongo and installed the mongo_mapper gem. Everything has been configured properly following this tutorial: http://www.mongodb.org/display/DOCS/Rails+3+-+Getting+Started. Then I struggled a little bit to allow Rails generate to work without throwing me the --orm not specified error. In order to get past this I had to add the rails3-generators gem and add it to the Gemfile.

Once all this was done, everything worked fine. I was able to successfully launch the Rails server.

I added a User controller thanks to the generate. The page works fine and even lists the users I have previously added:

alt text

However all the other actions, showing, editing, deleting, etc. are not working (creating works, but then it goes to show and the errors comes):

alt text

It's virtually the same error for all different actions.

The one difference I can notice right off the bat is that with the non MongoDB db, the id's of the user was starting at 1, etc. but here with MongoDB it looks like a randomly generated id that is much more complex and that is not of type int: 4d2ae91d4403baa84a000002

I am thinking that this may be creating the issues, since all action are using the id as a parameter... but I have no idea how to fix this. I have looked at the ruby generated code and it looks alright to me (extremely similar to the code generate for the default db).

Any help would be greatly appreciated ! I don't know how to go forward with my project without solving a simple generate code with mongodb.

Thanks,

Alex

ps:
please that I did not write any of this code at all. everything has been generated, which is kinda why I expected to work from the get go...

as asked here is the code for users_controllers:

class UsersController < ApplicationController
  # GET /users
  # GET /users.xml
  def index
    @users = User.all

respond_to do |format|
  format.html # index.html.erb
  format.xml  { render :xml => @users }
    end
  end

  # GET /users/1
  # GET /users/1.xml
  def show
@user = User.first(params[:id])

respond_to do |format|
  format.html # show.html.erb
  format.xml  { render :xml => @user }
end
  end

  # GET /users/new
  # GET /users/new.xml
  def new
@user = User.new

respond_to do |format|
  format.html # new.html.erb
  format.xml  { render :xml => @user }
end
  end

# GET /users/1/edit
  def edit
@user = User.first(params[:id])
  end

  # POST /users
  # POST /users.xml
  def create
    @user = User.new(params[:user])

respond_to do |format|
  if @user.save
    format.html { redirect_to(@user, :notice => 'User was successfully created.') }
    format.xml  { render :xml => @user, :status => :created, :location => @user }
  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
  end
end
end

  # PUT /users/1
  # PUT /users/1.xml
  def update
@user = User.first(params[:id])

respond_to do |format|
  if @user.update(params[:user])
    format.html { redirect_to(@user, :notice => 'User was successfully updated.') }
    format.xml  { head :ok }
  else
    format.html { render :action => "edit" }
    format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
  end
end
  end

  # DELETE /users/1
  # DELETE /users/1.xml
  def destroy
@user = User.first(params[:id])
@user.destroy

respond_to do |format|
  format.html { redirect_to(users_url) }
  format.xml  { head :ok }
end
  end
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ら栖息 2024-10-18 09:02:28

嗯,看来我找到了 pb...

我替换为:

@user = User.first(params[:id])

但是

@user = User.find(params[:id])

再次生成了此代码...那么错误来自哪里? Rails3-generators 中是否存在“错误”?还是我搞砸了这一代人?

亚历克斯

Hummm so it seems I found the pb...

I replaced:

@user = User.first(params[:id])

by

@user = User.find(params[:id])

But again, this code was generated... so where does the error come from ? Is there a "bug" in rails3-generators ? Or somehow I screwed up the generation ?

Alex

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文