Rails 3 - best_in_place 编辑

2024-01-28

希望有一个简单的答案;我正在使用宝石最佳地点 https://github.com/bernat/best_in_place而且效果很好。我试图弄清楚如何使用以下方法创建下拉菜单:

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

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

有什么想法如何做到这一点?我可以将它与collection_select混合使用吗?


: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 )。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Rails 3 - best_in_place 编辑 的相关文章

随机推荐