views:

457

answers:

4

Hello, I was curious on how to use arrays in the link_to method in ruby on rails for example:

Controller:

def index
    @test = [1,2,3]
end

View:

 <%= link_to "test", {:action => 'index'}, :test => @test %>

When looking at the source then, I end up with something to the effect of:

<a href="/index/" test="123">test</a>

My guess is that the array's to_string or something similar is getting called to set the value of test in the html.

My goal is to be able to have a form in which people can submit data on the page, and then once they've submitted the data and return to the page, if they click on the link the data will persist through clicking on the link.

*Ideally I would like to do this without having to pass the parameters in the url.

Thank you.

+1  A: 

My guess is that it is using Array#join.

You could try something like

:test => @test.join( ',' )

and then parse the string in your controller. But it is somewhat error prone if the user enters the same character you chose as delimiter.

But, assuming the linked page is also served by Rails, I think the best solution would be to use the flash area to store the results on the server

flash[ :submitted_params ] = params;

and in the controller for the linked page

old_params = flash[ :submitted_params ] || {}
Noel Walters
+1  A: 

If I understand well, you want to keep the datas submitted by the user after they validate the form ?
Well Rails is able to do that without any of your code line needed.

Based on the supposition that you have a route resource "objects"
In your controller :

def edit
    @object = Object.find_by_id params[:id]
end
def update
    @object = Object.find_by_id params[:id]
    if @object.update_attributes params[:object]
        # The datas have been successfully saved. You redirect wherever you want to.
    else
        render :action => 'edit'
    end
end

and in your view :

<% form_for @object do |f| %>
    <%= text_field :name %>
<% end %>

When the form fails to validate, the "name" text field automatically gets the previous entered data.

If after that you still need to reload your datas, you don't need to add them as a parameter in a link tag. You get the object in your controller and passes it's datas to the view where you display it.

Damien MATHIEU
+1  A: 

I would just write a view helper that formats it into a string with good separators, like commas.

That isn't a good way to be passing along information though. Try session variables, cookies, or url-encoded variables instead.

The best match to what you are doing would be url-encoded variables, which will show up in a form similar to this:

<a href="/index/?test=123">test</a>
Karl
+2  A: 

If you want to keep data you should probably use cookies. They are very easy to use, just assign a value with the following in the action:

cookies[:some_key] = "some value"

and retrieve it with this:

cookies[:some_key] # returns "some value"

However, just to clarify what link_to is doing in your example:

<%= link_to "test", {:action => 'index'}, :test => @test %>

When looking at the source then, I end up with something to the effect of:

<a href="/index/" test="123">test</a>

The reason is that you are passing @test to the third argument in link_to, which is a hash of html attributes, hence why it's turned into one. To have it become an parameter on the link, you need to pass it with the second, eg, {:action => 'index', :text => @test}. As noted above, however, this is not necessarily the best way to tackle this and, in addition, it's usually best to also pass the controller name to link_to or, better yet, use a named route.

autodata