0

I have a Controller with the function getAccounts where I look for certain accounts. My idea is to first show the number of results and then send the result array to the next function called showAccounts which generates the view. First of all I declared the result array as an instance variable. Then I tried to send with a form tag. It does not work ... Has anyone an idea?

  def getAccounts
filter = '(uid='+params[:id]+')'
attrs = ['*']
@accounts=Array.new

conn = LDAP::Conn.new($HOST, $PORT)
conn.bind('cn=admin, dc=cippool-mb, dc=rwth-aachen, dc=de','DLPins!')
conn.perror("bind")

begin
  conn.search($base, $scope, filter, attrs) { |entry|
    setAttributes(entry)
  }
 rescue LDAP::ResultError
   conn.perror("search")
   exit

 end
 conn.perror("search")
 conn.unbind


end

def showAccounts

end

The view where I send the data.

Es wurden <%= @accounts.size %> Accounts gefunden.  

<%= form_tag :action => "showAccounts" do %>
  <%= hidden_field_tag "accounts", @accounts %>
<%= submit_tag "Anzeigen" %>

<% end %>

I can also paste the view where I need this array, but I dont't think it's relevant for this question. I use Rails 3.2.7 and Ruby 1.9.2p0

2 Answers 2

1

If you want to pass some large amount of data between separate requests I would suggest using session, it's designed for such things.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanx, I try to read more about sessions and to establish a solution with that.
Thanx, it was very easy. I also switched to active record store.
1

If you debug(@accounts) you'll see what it passes -- something like <#0x7187237 Array> which is not what you want!

If you really want to pass in the accounts array, you'll need to serialize it to a text format to put in a hidden field. That's going to probably be a HUGE chunk of data though if @accounts is large.

That said, you could dump it to YAML or JSON, or use one of the serialization functions in Ruby or put it into a custom text format of your own (not recommended). Keep in mind then that you need to deserialize on the next page before you use it.

I'm assuming part of the wanting to pass it to the next step is to avoid an expensive LDAP request. You might want to look at putting in a lightweight cache -- redis for example -- to temporarily store the requests.

3 Comments

Thanx, I'll go through this. I temporarily fixed it with a global array, which stores the array. Then the other function takes it and saves it into a new instance variable.
@user1458773 Do you have single-threaded server? Otherwise global array doesn't work at all (bad idea in the first place anyway).
Oh yes, that's totally right. I forgot this. First I will use this solution to implement further parts, but I'll also read something about session to solve this problem more properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.