问题描述
||
如果有人能为我阐明这一点,我将非常感激。
我在Rails应用程序中有一个创建新书的表格。
class Book:
has_many :lnkbookauts,:foreign_key => \"LSA_BOOK_FK\",:dependent => :destroy
has_many :authors,:through => :lnkbookauts
class Author:
has_many :lnkbookauts,:foreign_key => \"LSA_AUT_FK\",:dependent => :destroy
has_many :books,:through => :lnkbookauts
在new.html.erb表单中,用户可以通过单击显示弹出窗口的按钮来添加作者。用户从该弹出窗口中选择作者,然后单击一个按钮,该按钮将关闭弹出窗口,并在以我的新书形式找到的列表框中显示选定的作者姓名。
所有这些都是使用javascript完成的。
单击“保存书籍”按钮可以将书籍条目与作者一起保存。
问题是,当我单击“编辑”按钮时,我希望作者姓名显示在我的编辑表单的列表框中,但不幸的是,当前情况并非如此。
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\']%>
我的edit.html.erb表单也包含用于作者的相同代码。
编辑表单中是否缺少某些内容?
解决方法
您需要告诉编辑局部从哪里读取那个
select
的options
。尝试将以下内容放入<select>...</select>
标记中:
<%= options_from_collection_for_select(@book.authors,\"id\",\"name\") %>
(在我假设@book
是用于构建表单的Book实例的情况下,您希望每个Author
的id
成为选项的value
字段,并且Author
具有name
方法将提供显示的字符串)
看这里