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.
Best Answer
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:
and retrieve it with this:
However, just to clarify what
link_to
is doing in your example: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.