Rails 3 – best_in_place编辑

希望是一个简单的答案; 我正在使用gem best_in_place ,效果很好。 我正在尝试使用以下方法找出如何创建下拉菜单:

:type => :select, :collection => [] 

我想要做的是传入从我的用户模型输入的名称列表。

有什么想法怎么做? 我可以将它与collection_select混合使用吗?

:collection参数接受键/值对的数组:

  [ [key, value], [key, value], [key, value], ... ] 

其中选项值选项文本

最好在模型中生成此数组,该模型对应于要为其生成选项列表的对象,而不是在视图中。

听起来你有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 (eg show.html.erb) ## excerpt 

Assigned to: <%= best_in_place @project, :user_id, :type => :select, :collection => User::list_user_options %>

# note :user_id and not :user

请注意,从内存中, best_in_place的主版本发送选择框的ajax请求,无论值是否更改。

还要记住一些事情; best_in_place用于“现场”编辑现有记录,而不是创建新记录(为此,在_form partial中使用collection_select用于新页面)。