# Rendering a partial from within that partial

**URL:** <https://www.ruby-forum.com/t/rendering-a-partial-from-within-that-partial/115139>\
**Category:** Rails\
**Created:** [September 20, 2007, 11:24pm UTC](https://www.ruby-forum.com/t/rendering-a-partial-from-within-that-partial/115139 "2007-09-20T23:24:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [September 20, 2007, 11:24pm UTC](https://www.ruby-forum.com/t/rendering-a-partial-from-within-that-partial/115139/1 "2007-09-20T23:24:28Z")

</div>

I am having trouble rendering a partial called ‘names’ through a submit  
tag located within that partial. The form data is saving with my current  
code. However, the existing data is not loading into the form and the  
partial is not being re-rendered upon submission to reveal the changes  
to this data.What might I do to fix this?

show.rhtml

\<%= javascript\_include\_tag :defaults %\>

\<%= render :partial =\> 'names', :object =\> @person %\>

\_names.rhml (lists the names of ‘Person’. Clicking on a name reveals a  
form to edit that name)

\<% for name in @person.current\_names %\>  
\<%= link\_to\_function (name.full\_name, :onclick =\>  
“Element.show(‘edit\_name\_#{claimantname.id}’)” %\>

 \<%= form\_remote\_tag(:url =\> {:action =\> 'update\_name', :id =\> name} )%\> \<%= text\_field 'name', 'first\_name' %\> \<%= text\_field 'name', 'last\_name' %\> \<%= submit\_tag 'Edit', :onclick =\> "Element.hide('edit\_name\_#{name.id}')" %\> \<%= end\_form\_tag %\>
\<% end %\>

persons\_controller

def update\_name  
@name = Name.find(params[:id])  
if @name.update\_attributes(params[:name])  
return if request.xhr?  
render :partial =\> ‘names’  
end  
end

update\_name.rjs

Code : ruby - fold - unfold  
page.replace\_html(“name\_list”, :partial =\> “names”, :object =\> @person)

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [September 21, 2007, 2:15am UTC](https://www.ruby-forum.com/t/rendering-a-partial-from-within-that-partial/115139/2 "2007-09-21T02:15:23Z")

</div>

> - There’s no @person instance variable defined by the update\_name  
> action.
> - text\_field ‘name’, ‘last\_name’ assumes there’s an instance variable  
> @name, which there isn’t in your case. One way round it is to do this
> 
> \<% form\_remote\_for(:name, name, :url =\> {:action =\> ‘update\_name’, :id  
> =\> name}) do |f| %\>  
> \<%= f.text\_field :first\_name %\>  
> …  
> \<% end %\>
> 
> Fred

Right on about the @person instance variable. form\_remote\_for used as  
you suggested loads my data into the fields fine:

\<% form\_remote\_for(:name, name, :url =\> {:action =\> ‘update\_name’, :id  
=\> name}) do |f| %\>  
\<%= f.text\_field :first\_name %\>  
\<%= f.text\_field :last\_name %\>  
\<%= submit\_tag ‘Edit’ %\>  
\<% end %\>

However, it no longer saves my changes. Is there something I need to  
change in the controller or submit tag as well?

Thanks for your help.

Peter

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [September 20, 2007, 11:46pm UTC](https://www.ruby-forum.com/t/rendering-a-partial-from-within-that-partial/115139/3 "2007-09-20T23:46:24Z")

</div>

> def update\_name  
> @name = Name.find(params[:id])  
> if @name.update\_attributes(params[:name])  
> return if request.xhr?  
> render :partial =\> ‘names’
> 
> update\_name.rjs
> 
> page.replace\_html(“name\_list”, :partial =\> “names”, :object =\> @person)

- There’s no @person instance variable defined by the update\_name  
action.
- text\_field ‘name’, ‘last\_name’ assumes there’s an instance variable  
@name, which there isn’t in your case. One way round it is to do this

\<% form\_remote\_for(:name, name, :url =\> {:action =\> ‘update\_name’, :id  
=\> name}) do |f| %\>  
\<%= f.text\_field :first\_name %\>  
…  
\<% end %\>

Fred
