# Edit form updating with text

**URL:** <https://www.ruby-forum.com/t/edit-form-updating-with-text/104551>\
**Category:** Rails\
**Created:** [May 19, 2007, 10:01am UTC](https://www.ruby-forum.com/t/edit-form-updating-with-text/104551 "2007-05-19T10:01:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![NickT](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ce7236/32.png) [@NickT](https://www.ruby-forum.com/u/NickT)\
**Post date:** [May 19, 2007, 10:01am UTC](https://www.ruby-forum.com/t/edit-form-updating-with-text/104551/1 "2007-05-19T10:01:11Z")

</div>

So I think I’m making this a lot harder than it really is. I am still  
learning the ins and out of Rails so and I think I’m just missing  
something easy.

I am trying to create an edit form that allows a user to select a photo  
album name, but rather than having them go to a new page I have the  
first form post to the same page (I don’t know if this would be easier  
in AJAX if so any help here would be great since I am in the process of  
learning that as well). When the page reloads the new form is displayed.  
I have followed several examples and when they do work the form fields  
are empty. Here is the code:

Album.rhtml

…  
\<% if request.post? %\>

 \<% disp = params[:album] %\> Album Param: \<%= disp %\> # used this to verify that the correct id was being sent to the form and it currently works \<% form\_for :album, :action =\> 'update\_album', :id =\> params[:album] do %\> \<%= render :partial =\> 'form' %\> \<%= submit\_tag "Apply Changes" %\> \<% end %\> \<% else %\> \<% form\_for :album, :url =\> { :action =\> 'album', } do |form| %\> \<%= @albums = Album.list\_albums form.collection\_select('' , @albums, :id, :album\_name)%\> \<%= submit\_tag "Update Album", :confirm =\> 'Are you sure?' %\> \<% end %\> \<% end %\> ...

\_form.rhtml \<-- I believe this is the standard \_form.rhtml from the  
rails scaffold

\<%= error\_messages\_for ‘album’ %\>

Name  
\<%= text\_field 'album', 'album\_name' %\> Created date  
\<%= date\_select 'album', 'created\_date' %\> Hide  
\<%= check\_box 'album', 'hide' %\> Private  
\<%= check\_box 'album', 'private' %\> \<%= hidden\_field 'album', 'user\_id', :value =\> 1 %\>

Any help is greatly apriciated.

---

<div class="post-metadata">

**Author:** ![NickT](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ce7236/32.png) [@NickT](https://www.ruby-forum.com/u/NickT)\
**Post date:** [May 23, 2007, 9:30am UTC](https://www.ruby-forum.com/t/edit-form-updating-with-text/104551/2 "2007-05-23T09:30:20Z")

</div>

NickT

Your error is coming from you thinking about handling the response in  
the template instead of the controller.

The data the form helpers display comes from the Model objects  
(usually from database tables). So you need to make sure that the  
update\_album method in the controller is correctly initialising an  
instance of the album class (model). eg. @album contains valid  
data.

Overall, as a structural suggestion, take the two parts of the form in  
your if condition, and make them separate partials, eg \_update\_detail  
and \_list\_detail. Then, in the controller methods album and  
update\_album set up a control variable eg.  
@album\_detail=‘update\_detail’ or @album\_detail=‘list\_detail’

Then you can replace all of the code between the if/end in your view  
with render :partial=\>@album\_detail

This puts all your logic back into the controller and removes the need  
to test for request? and also gives you an arrangement that readily  
lends itself to an Ajax call. You only have to put an id around the  
render statement and use that as the update parameter in the ajax  
call. The controller can then render :partial to do the update.

BTW, you are using a table for layout purposes, and whilst I am not  
obsessed with tableless layout, in this instance it will give you a  
problem because you will find it difficult to wrap two table rows with  
a common id for ajax update. You may get away with a , but the  
browser may not like updating parts of the table in this way. I have  
an app that solely uses Firefox, and that doesnt mind if you use ajax  
to replace a whole row, using an id in the , but I havnt tried  
spanning multiple rows.

hth Tonypm
