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)..
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
alert('<%= @listofbooks %>');
@listofbooks
only shows the title of the book I last added..
The first() is an inbuilt method in Ruby returns an array of first X elements. If X is not mentioned, it returns the first element only. Syntax: range1.first(X) Parameters: The function accepts X which is the number of elements from the beginning. Return Value: It returns an array of first X elements.
Ruby arrays can hold objects such as String, Integer, Fixnum, Hash, Symbol, even other Array objects. Ruby arrays are not as rigid as arrays in other languages. Ruby arrays grow automatically while adding elements to them.
Array methods in Ruby are essentially objects that can store other objects. You can store any kind of object in an array. You can create an array by separating values by commas and enclosing your list with square brackets. In Ruby, arrays always keep their order unless you do something to change the order.
This can be done in a few ways in Ruby. The first is the plus operator. This will append one array to the end of another, creating a third array with the elements of both. Alternatively, use the concat method (the + operator and concat method are functionally equivalent).
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.
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, libraryshelf
is a local variable, and on its first reference in the method it will always not be defined.
Operate directly on @listofbooks
and just check @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.
The original post kind of fooled us by also clobbering @listofbooks
every time through the method, so we thought it was really a ruby question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With