# Partial templates question

**URL:** <https://www.ruby-forum.com/t/partial-templates-question/132592>\
**Category:** Rails\
**Created:** [March 21, 2008, 6:30pm UTC](https://www.ruby-forum.com/t/partial-templates-question/132592 "2008-03-21T18:30:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [March 21, 2008, 6:30pm UTC](https://www.ruby-forum.com/t/partial-templates-question/132592/1 "2008-03-21T18:30:58Z")

</div>

hi, i have a question about partial templates.

there is a table - Topics, it has one field - Title.

there is a text\_field form with button, and a partial template under the  
form. they are in the same template. the partial can display Titles  
which is already in the Topics table.

user enter a string in the form, click the button. the string will save  
as Title in Topics table. the partial will display all the data from the  
Topics table, including the string which user just entered.

i have been thinking all day, but can’t find a solution. can someone  
help, give me a solution. thanks very much!

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [March 21, 2008, 7:04pm UTC](https://www.ruby-forum.com/t/partial-templates-question/132592/2 "2008-03-21T19:04:55Z")

</div>

Bob Song wrote:

> hi, i have a question about partial templates.
> 
> there is a table - Topics, it has one field - Title.
> 
> there is a text\_field form with button, and a partial template under the  
> form. they are in the same template. the partial can display Titles  
> which is already in the Topics table.
> 
> user enter a string in the form, click the button. the string will save  
> as Title in Topics table. the partial will display all the data from the  
> Topics table, including the string which user just entered.
> 
> i have been thinking all day, but can’t find a solution. can someone  
> help, give me a solution. thanks very much!

I am doing pretty much this exact same thing. What I have done is to  
wrap my partials inside of divs with an id value  
‘name\_of\_div\_where\_partial\_is’. I used the form\_remote\_tag helper to add  
to the list without reloading the whole page. Then you can do one of two  
things:

1. In the method that is called when you hit the submit button you save  
the new Title and then quary to get the old titles and the new title.  
Then, you should have the partial already set-up so that variable is  
being used to dsiplay the title in that partial. Add the :update option  
to the form\_remote\_tag and it should update without reloading the whole  
page:

\<%= form\_remote\_tag :update =\> ‘name\_of\_div\_where\_partial\_is’, . . . %\>

or,

1. In the method that gets called when you hit the submit button you can  
use the render method with the :update attribute. So something like:

render :update do |page|  
page.replace\_html ‘name\_of\_div\_where\_partial\_is’, :partial =\>  
‘name\_of\_partial’  
end

What this does it is says to redisplay the partial in the div  
overwriting the html currently there.

I use the second way to update a group of two lists that I have on a  
single page, where I remove one item from a list that then automatically  
gets added to the second list of removed stuff. Hope this helps

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [March 22, 2008, 1:32am UTC](https://www.ruby-forum.com/t/partial-templates-question/132592/3 "2008-03-22T01:32:01Z")

</div>

thanks Shandy N.! it really useful!  
following is my code

Controller:  
def index  
@topic = Topic.new(params[:topic])  
if request.post?  
@user.save  
end

```
@topics = Topic.find(:all)

```

end

View:  
\<% remote\_form\_for :topic, :update =\> ‘name\_of\_div\_where\_partial\_is’ do  
|f| %\>  
\<%= f.text\_field :title %\>  
\<%= submit\_tag “Add” %\>  
\<% end %\>

\<%= render(:partial =\> "show", :collection =\> @topics) %\>

Partial:  
\<%= show.title%\>

* * *
\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_

one more question. if i would like to highlight the new line. how can i  
do?

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [March 22, 2008, 1:40am UTC](https://www.ruby-forum.com/t/partial-templates-question/132592/4 "2008-03-22T01:40:48Z")

</div>

i created a highlight.rjs file, which including highlight javascript  
code. the highlight code needs to know which is the new entered object.  
so i transfered the new object’s id to it.

View:  
\<%= javascript\_include\_tag :defaults %\>  
\<% remote\_form\_for :user, :url =\> {:action =\> :highlight, :id =\> @user},  
:update =\> ‘name\_of\_div\_where\_partial\_is’ do |f| %\>  
\<%= f.text\_field :name %\>  
\<%= submit\_tag “Add” %\>  
\<% end %\>

\<%= render(:partial =\> "show", :collection =\> @users) %\>

Controller:  
def index  
@user = User.new(params[:user])  
if request.post?  
@user.save  
end  
@users = User.find(:all)  
end

def highlight  
@user = User.find(params[:id])  
end

loading the index page, it works well. but after i enter a new name,  
click submit, partial shows “Couldn’t find User without an ID”. the user  
ID didn’t transfer to the highlight method
