dracos
September 20, 2007, 11:24pm
1
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 )
dracos
September 21, 2007, 2:15am
2
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
dracos
September 20, 2007, 11:46pm
3
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