views:

831

answers:

3

I have been trying to get my head around render_to but I haven't had much success.

Essentially I have controller methods:

def first
  #I want to get the value of VAR1 here
end

def second
  VAR1 = ["Hello", "Goodbye"]
  render_to ??
end

What I can't figure out is how to accomplish that. Originally I just wanted to render the first.html.erb file but that didn't seem to work either.

Thanks

Edit: I appreciate the answers I have received, however all of them tend to avoid using the render method or redirect_to. Is it basically the case then that a you cannot pass variables from controller to controller? I have to think that there is some way but I can't seem to find it.

+2  A: 

I usually don't have my controllers calling each other's actions. If you have an identifier that starts with a capital letter, in Ruby that is a constant. If you want to an instance level variable, have it start with @.

@var1 = ["Hello", "Goodbye"]

Can you explain what your goal is?

Andy Gaskell
Essentially I am trying to do a multi-step process where a user uploads a file, then I process it and re-present it back to them to let them make certain changes to it, and then finally insert it into a database. To do that though I need to be able to pass that object to another controller or at least another controllers view.
John Baker
Rather than doing that why not store the intermediary results of your workings in the database and flag it as incomplete. That way you know you get to access it through the model as normal and you are resilient to server failures.
Paul Keeble
+2  A: 

It is not a good idea to assign the object to a constant. True this is in a global space, but it is global for everyone so any other user going to this request will get this object. There are a few solutions to this.

I am assuming you have a multi-step form you are going through. In that case you can pass the set attributes as hidden fields.

<%= f.hidden_field :name %>

If there are a lot of fields this can be tedious so you may want to loop through the params[...] hash or column_names method to determine which attributes to pass.

Alternatively you can store attributes in the session.

def first
  @item = Item.new(params[:item])
  session[:item_attributes] = @item.attributes
end

def second
  @item = Item.new(session[:item_attributes])
  @item.attributes = params[:item]
end

Thirdly, as Paul Keeble mentioned you can save the model to the database but mark it as incomplete. You may want to use a state machine for this.

Finally, you may want to take a look at the Acts As Wizard plugin.

ryanb
A: 

Have you considered using the flash hash? A lot of people use it solely for error messages and the like, it's explicitly for the sort of transient data passing you might be interested in.

Basically, the flash method returns a hash. Any value you assign to a key in the hash will be available to the next action, but then it's gone. So:

def first
  flash[:var] = ["hello", "goodbye"]
  redirect_to :action => :second
end

def second
  @hello = flash[:var].first
end
Judson