Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems keeping an object in an array, Ruby issues and Rails issues

Tags:

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..

like image 954
thedeepfield Avatar asked Jan 29 '11 06:01

thedeepfield


People also ask

What does .first mean in Ruby?

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.

How does array work in Ruby?

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.

Are arrays objects in Ruby?

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.

Can you add arrays in Ruby?

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).


1 Answers

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.

like image 133
DigitalRoss Avatar answered Nov 02 '22 10:11

DigitalRoss