:collection 参数接受键/值对数组:
[ [key, value], [key, value], [key, value], ... ]
哪里的key is the 期权价值 and value is the 选项文本.
最好在与您要为其生成选项列表的对象相对应的模型中生成此数组,而不是在您的视图中。
听起来您已经启动并运行了 best_in_place,因此这是一个项目显示页面的简单示例,您希望在其中使用 best_in_place 通过选择框更改特定项目的分配用户。
## CONTROLLER
# GET /projects/1
# GET /projects/1.xml
# GET /projects/1.json
def show
@project = Project.find(params[:id])
respond_to do |format|
format.html
format.xml { render :xml => @project.to_xml }
format.json { render :json => @project.as_json }
end
end
## MODELS
class User
has_many :projects
def self.list_user_options
User.select("id, name").map {|x| [x.id, x.name] }
end
end
class Project
belongs_to :user
end
## VIEW (e.g. show.html.erb)
## excerpt
<p>
<b>Assigned to:</b>
<%= best_in_place @project, :user_id, :type => :select, :collection => User::list_user_options %>
</p>
# note :user_id and not :user
请注意,根据记忆,主版本最佳地点 http://github.com/bernat/best_in_place无论值是否更改,都会发送对选择框的 ajax 请求。
还有一些需要记住的事情; best_in_place 用于“就地”编辑现有记录,而不是创建新记录(为此,请在新页面的 _form 部分中使用 collection_select )。