Retuning Hash variable over POST

Hi,

I am passing a hash variable : @websites{"link_test" => "Ruby Forum", "url" => "http://www.ruby-forum.com" } down to a RHTML that lists all these urls with checkboxes next to it.

Idea is to let users choose the URLs that they want to bookmark. So when users select the URLs they want, and click Save (Post Method), I want to pass on the same Hash variable back and levrage Checkboxes IDs to learn wht IDs he had selected.

But when i do that, the hash variable is passed as String: websites will have "link_test Ruby Forum http://www.ruby-forum.com".

How can I avoid this?

Regards, Sandeep G

I'd suggest identifying an id that can be used with each url. If you're storing the urls in a table then the id is easy enough. If not, you should create a 'key' by either storing the urls in a constant array (key = index) or hash (add the id to the hash for each url).

Once you have an id, you should be able to build your checkboxes using the :index attribute and assign the id as the index. On postback you'll get an array of arrays where the first element of the inner array is the id... that's really all you need (since unchecked checkboxes should not be posted back).

Thanks but i am afraid that wont hlp much. I must have made it clear. URLs are actually pulled from some other site and i wudnt have URL database at all.Mebbe I can store it temporary n all that, but i was wondering i have a solution to avoid my situation," Hash getting converted into String while I post"

Regards, Sandeep

What's in your view and controller (and model, while we're at it)?

-Kyle

Hi Kyle,

This is how it is:

In the controller: action: select_url

I am parsing a page from different website which contains URLS, and make a hash of such URLs in the format of @urls[0] = {"link_test" => "rubyforum","URL" => "ruby-forum.org"} @urls[1] = {"link_test" => "railsforum","URL" => "railsforum.org"} . LIke that.

View: I just list all URLs with a checkbox next to it.

When I post, I pass on hash of urls by doing this in form_tag :urls => @urls for the action save_urls

In the controller: action save_urls:

I tried to parse thru @urls, thinking that i would still have hash(because this is same variable that passed on to view) but unfortunately i have string.

I installed json and tried to convert string to hash like this: @urls = $H(urls.evalJSON()) but got syntax error :frowning:

@Sandeep -- somehow, some way you are creating a collection of websites. Based on your post above we'll assume it's @websites.

<ul> <% @websites.each_with_index do |website_hash, index %> <%= check_box_tag ... :index=>index %> <% end %> </ul>

.

Hi Andy,

Thanks. But issue I am facing is not listing of URLS but rather posting the urls back.

Action View: select_urls <%= form_tag ({:controller => "import" ,:action => :save_urls , :urls => @urls })%>

% i =0 %> <% for url in @websites %> <label><%= check_box(:url, :i, :name => "url[#{i}]" ,:checked => true)%></label> <div class="enable-disable"> <%= url["url"] %><br/> <% i = i + 1 %> <br/>

<%end %></div> <br/>

<% end_form_tag %>

Issue is @urls, that i pass down to view is a array of hashes. I am posting the same variable back on submit. Here, array of hashes is getting converted into string. This is the issue.

Regards, Sandeep G

AndyV wrote:

If I understand your question... you can't pass back a hash from the browser to Ruby. The browser will always pass back a string. The trick is to create a string that you can parse into a hash, then do what you want with that hash. There's a few ways to do that.

If the checkboxes all have the same name="" then you're going to get one string back with the contents of all the value="" attributes put together delimited by EOL characters. If you need each checkbox to have multiple values you can separate into hash keys, you'll have to see to it that the value="" attribute is something like value="link_test:::Ruby Forum:::ttp://www.ruby-forum.com" so you can parse at the ::: (or whatever delimiter you like).

It would be helpful to see the HTML of these checkboxes as it appears in your web page sourcecode, and an example of the hash structure you're wanting to have back when there are multiple selections made.

@Sandeep:

I understand your issue. The approach that I am suggesting will achieve exactly what you are after (note that each_with_index is a cleaner version of the for loop with a counter that you're using). Now that I finally see your code it appears that the problem lies in the way you are trying to force the url collection back to the controller on submit. I will suggest that you don't _really_ want the collection, you only want the selected urls. Get rid of the last parameter on your form_tag and leverage the checkbox to return the url instead.

<%= form_tag ({:controller => "import" ,:action => :save_urls)%>

<% websites.each_with_index do |url, i| %>   <%= check_box_tag("url[#{i}][bookmark]", :checked => true, :value=>url[:URL])%>   <label for="<%="url[#{i}][bookmark]"%>" class="enable-disable"><%= url[:link_test] %></label> <%end %> <% end_form_tag %>

That's untested but the result should be that you get a params[:url] that is an array of arrays containing only the checked items. The first item of the inner array will be the index that was generated via each_with_index. The second item will be a hash with :bookmark=>'some url to bookmark'.

Yea true. Got it. Thanks a lot Andy.

Regards, Sandeep G

AndyV wrote: