# How can I access currently selected records in the view?

**URL:** <https://www.ruby-forum.com/t/how-can-i-access-currently-selected-records-in-the-view/160767>\
**Category:** Rails\
**Created:** [February 19, 2009, 2:31pm UTC](https://www.ruby-forum.com/t/how-can-i-access-currently-selected-records-in-the-view/160767 "2009-02-19T14:31:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sa125](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@sa125](https://www.ruby-forum.com/u/sa125)\
**Post date:** [February 19, 2009, 2:31pm UTC](https://www.ruby-forum.com/t/how-can-i-access-currently-selected-records-in-the-view/160767/1 "2009-02-19T14:31:08Z")

</div>

One of my views enables searching records using a text field. The  
rendered view contains only records that match the search.

After the view has rendered, I want to add an option to perform an  
update on the displayed records. The problem is, I don’t know how to  
figure out which records are shown, since the search form is different  
from the update form, and I can only submit one of them.

Any ideas?

---

<div class="post-metadata">

**Author:** ![sa125](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@sa125](https://www.ruby-forum.com/u/sa125)\
**Post date:** [February 19, 2009, 2:39pm UTC](https://www.ruby-forum.com/t/how-can-i-access-currently-selected-records-in-the-view/160767/2 "2009-02-19T14:39:41Z")

</div>

Let us assume you are displaying search results using @mysearchlist and  
iterate through them.

\<% @mysearchlist.each do |a| %\>  
\<%= link\_to a.title, :action =\> “show”, :id =\> a.id %\>  
\<%= link\_to ‘edit’, :action =\> “edit”, :id =\> a.id %\>  
\<% end %\>

There you go!.

---

<div class="post-metadata">

**Author:** ![sa125](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@sa125](https://www.ruby-forum.com/u/sa125)\
**Post date:** [February 19, 2009, 3:38pm UTC](https://www.ruby-forum.com/t/how-can-i-access-currently-selected-records-in-the-view/160767/3 "2009-02-19T15:38:42Z")

</div>

sa 125 wrote:

> Now, a separate form on the page is used to update the shown records. I  
> can’t see how this relates to your answer (then again, I’m new to all  
> this).

If you have a separate form on the page for edits, how are you getting  
the “selected” record into that form for editing?

I assume there a list view at the top which returns all those record  
meeting the search criteria, and below is the editable form.

Do you edit one by one, or are you looking for something more like an  
edit in place within the listing?

Haven’t done this myself, so this is all speculation…

For one by one, I assume you’ll need some column in the list partial  
where the action (on click) executes a remote call with that record id  
and loads that records data into the form portion for editing (discard  
what was in the form portion, and reload that form part with the  
“current” records data). Or something like that?

---

<div class="post-metadata">

**Author:** ![sa125](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@sa125](https://www.ruby-forum.com/u/sa125)\
**Post date:** [February 19, 2009, 2:50pm UTC](https://www.ruby-forum.com/t/how-can-i-access-currently-selected-records-in-the-view/160767/4 "2009-02-19T14:50:01Z")

</div>

Thanks for the quick response.

I’m not sure I follow. My view is rendered with partials, called from  
search function when the user submits the search:

\<%= render :partial =\> ‘record’, :collection =\> @search\_results %\>

–

def search\_records

# get the search form results from params

conds = # formulate the conditions  
@search\_results = Records.find(:all, :conditions =\> conds)

# render the view

end

Now, a separate form on the page is used to update the shown records. I  
can’t see how this relates to your answer (then again, I’m new to all  
this).

---

<div class="post-metadata">

**Author:** ![sa125](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@sa125](https://www.ruby-forum.com/u/sa125)\
**Post date:** [February 19, 2009, 4:58pm UTC](https://www.ruby-forum.com/t/how-can-i-access-currently-selected-records-in-the-view/160767/5 "2009-02-19T16:58:19Z")

</div>

> Do you edit one by one, or are you looking for something more like an  
> edit in place within the listing?

It’s more like finding the records to update using a filtering form, and  
then updating some or all the attributes in these records using another  
update form. More code and explanations in this post:  
[http://railsforum.com/viewtopic.php?id=27309](http://railsforum.com/viewtopic.php?id=27309)  
Feel free to answer there, and I’ll link it here.  
Thanks!
