list box in rails - data not displayed in listbox when editing form
I would be really grateful if someone could shed some light on this for me.
I have in my Rails application a form to create new books.
class Book:
has_many :lnkbookauts, :foreign_key => "LSA_BOOK_FK", :dependent => :destroy
has_many :authors, :through 开发者_Python百科=> :lnkbookauts
class Author:
has_many :lnkbookauts, :foreign_key => "LSA_AUT_FK", :dependent => :destroy
has_many :books, :through => :lnkbookauts
In the new.html.erb form, user can add authors by clicking on a button which displays a popup. User selects the authors from that popup and then clicks on a button which closes the popup and displays the selected author names in a listbox found in my new book form. All this is done using javascript.
Clicking of the 'Save Book' button saves the book entry along with the authors.
The problem is When i click on the 'Edit' button, i would like the author names to be displayed in the list box on my edit form but unfortunately currently, that's not the case.
new.html.erb:
<p class="redLink">List of Authors:- <span><select size=5 id="book_author_ids" name="book[author_ids][]" multiple selected="true"> </select></span>
<br/>
<%= link_to add authors, new_author_path, :popup => ['new_window_name', 'height=400 px, width=500 px']%>
my edit.html.erb form also contains the same code for authors.
Is there something missing in the edit form ?
You need to tell the edit partial where to read the options
for that select
from. Try putting the following inside the <select>...</select>
tag:
<%= options_from_collection_for_select(@book.authors, "id", "name") %>
(Where I'm assuming that @book
is the Book instance being used to build the form, that you want the id
of each Author
to be the value
field of the option, and that Author
has a name
method which will provide the displayed string.)
See here
精彩评论