Edit form updating with text

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:


<% 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’ %>

<%= 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.


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

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