将对象保存在数组中的问题、Ruby 问题和 Rails 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4835536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Problems keeping an object in an array, Ruby issues and Rails issues
提问by thedeepfield
I'm trying to add an object to my array, however the array seems to always reset, instead of adding. What am I doing wrong? I think it has to do with if(defined? libraryshelf) then, What I'm trying to do here is find out of the array exists or not (if this is the first add or not)..
我正在尝试将一个对象添加到我的数组中,但是该数组似乎总是重置,而不是添加。我究竟做错了什么?我认为这与if(defined? libraryshelf) then,我在这里要做的是找出数组是否存在(如果这是第一个添加与否)..
def add_book
@listofbooks ||= Array.new
@listofbooks.push(params[:booktitle])
@listofbooks
respond_to do |format|
format.html { redirect_to(:back) }
format.js
end
end
my add_book.js.erb file
我的 add_book.js.erb 文件
alert('<%= @listofbooks %>');
@listofbooksonly shows the title of the book I last added..
@listofbooks只显示我上次添加的书名..
回答by DigitalRoss
TL;DR:The controller's are stateless, they just see the incoming request. To have a list outlive the current request, you need to persist the list in either the session or in the database, depending on how long you want it to live and other considerations.
TL;DR:控制器是无状态的,他们只看到传入的请求。要使列表比当前请求更有效,您需要将列表保存在会话或数据库中,具体取决于您希望它存活多长时间和其他考虑因素。
There are some other issues...
还有一些其他问题...
Don't used defined?for that, in fact, don't use defined?for anything. It doesn't have many legit application-level uses. In this case, libraryshelfis a local variable, and on its first reference in the method it will always not be defined.
不要用于defined?那个,事实上,不要defined?用于任何东西。它没有很多合法的应用程序级用途。在这种情况下,libraryshelf是一个局部变量,并且在方法中的第一次引用时,它永远不会被定义。
Operate directly on @listofbooksand just check @listofbooks or @listofbooks.nil?.
直接操作@listofbooks,只需检查@listofbooks or @listofbooks.nil?。
Here are a few working (I think) versions...
这里有一些工作(我认为)版本......
def add_book name
@listofbooks = [] unless @listofbooks
@listofbooks << name
end
def add_book name
@listofbooks ||= []
@listofbooks << name
end
def add_book name
@listofbooks = @listofbooks.to_a.push name # yes, works even if @listofbooks.nil?
end
Aha, your revised post is better ... as noted in TL;DR: because Rails recreates controller objects on each request, you will need to persist anything you want next time around in your session or database.
啊哈,你修改后的帖子更好......正如 TL;DR 中所述:因为 Rails 会在每个请求上重新创建控制器对象,所以下次你需要在会话或数据库中保留任何你想要的东西。
The original post kind of fooled us by alsoclobbering @listofbooksevery time through the method, so we thought it was really a ruby question.
原帖那种被愚弄我们也重挫@listofbooks通过方法每一次,所以我们认为这是一个真正的红宝石问题。
回答by EnabrenTane
since you're in a function libraryshelfwill never be defined as a local variable. And I'm guessing you are using Ruby 1.8.7 so then you are making a new array in a scope (that you shouldn't be able to see ) and assigning that to @listofbooks
因为你在一个函数中libraryshelf永远不会被定义为局部变量。我猜你正在使用 Ruby 1.8.7 所以你正在一个范围内创建一个新数组(你不应该看到)并将它分配给@listofbooks
I suggest
我建议
def add_book
@listofbooks ||= Array.new
@listofbooks.push(name)
@listofbooks # return the entire list, not the last thing pushed
end
Edit to reflect updated question
编辑以反映更新的问题
This is a problem with the life cycle of the controller. For each request a new controller object is instantiated so any @variables are cleared between requests. You will need to store your variables by something like session[:booklist] = @booklist, then retrieve it for the next request.
这是控制器生命周期的问题。对于每个请求,都会实例化一个新的控制器对象,以便在请求之间清除所有 @variables。您需要通过类似的方式存储变量session[:booklist] = @booklist,然后为下一个请求检索它。
回答by rynmrtn
Looking at the code, it seems the logic written differs from the description of what you want to happen. This is what I see your code is saying:
查看代码,似乎编写的逻辑与您想要发生的事情的描述不同。这就是我看到你的代码所说的:
# Create an Empty Array, assign it to @listofbooks
@listofbooks ||= Array.new
# Add the name of a book from a request parameter
@listofbooks.push("Agile Development with Rails")
# At this point @listofbooks only contains 1 element; we started with
# an empty array and added 1 element through the request parameter
However, it sounds like you want to do the following:
但是,听起来您想要执行以下操作:
def add_book
# Simply push the new book title on the array of old books
@listofbooks << params[:booktitle]
end

