Sunday 15 August 2010

Rails - first item in form select controls after edit/update -


When a user user edits the event, they are redirected to the user page, which links to their user events it shows.

After updating, if they click that link again for editing, they edit the first value for editing, not as the new value selected in the last edit.

New data is actually being saved in the database.

This is only for selected controls, changes made in text fields and checkboxes fix new values.

Therefore always shows the first item of selection.

  & lt;% = f.label: title, "title (max 150 characters)"%> & Lt;% = f.text_area: Title% & gt; & Lt; Div & gt; & Lt;% = f.label: user_event_type, "Event Type"% " (Prompt: "Choose an Event Type"), } & Gt%; & Lt; / Div & gt; Def edit @user_event = userEvent.find (params [: id]) end def update @user_event = UserEvent.find (params [: id]) if @ user_event.update_attributes (params [: user_event]) flash [: success] = " The event successfully renders "redirect_to user_url (current_user) and 'edit and end'    

The post responded by:

Simply remove options_for_select, and simply pass the 2-D array in the selection method as the second argument, it will be automatically selected .

Before:

  & lt;% = f.select: state, options_for_select ([['Alaska' ', 1], [' Florida ', 2], ['Texas' 3]])% & gt; After   

:

  & lt;% = f.select: state, [['Alaska', 1], ['Florida', 2] ], ['Texas', 3]]%>    

No comments:

Post a Comment