Rails 3 - best_in_place 编辑

发布于 2024-10-31 03:12:15 字数 301 浏览 4 评论 0原文

希望有一个简单的答案;我正在使用 gem best_in_place 并且效果很好。我试图弄清楚如何使用以下方法创建下拉菜单:

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

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

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

Hopefully a simple answer; I am using the gem best_in_place and it works great. I'm trying to figure out how to create a drop down menu using:

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

What I want to be able to do is pass in a list of names that have been entered from my user model.

Any thoughts how to do this? Can I mix it with collection_select?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无戏配角 2024-11-07 03:12:15

: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 (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

请注意,根据记忆, best_in_place 的主版本会发送选择框的 ajax 请求,无论值是否更改或不。

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

The :collection parameter accepts an array of key/value pairs:

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

Where the key is the option value and value is the option text.

It is best to generate this array in the model corresponding to the object for which you want to generate a list of options for, and not in your view.

Sounds like you have best_in_place up and running, so here's a simple example of a project show page, where you want to use best_in_place to change the assigned user for a particular project with a select box.

## 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

Note that from memory, the master version of best_in_place sends the ajax request for a select box whether the value is changed or not.

Also something to keep in mind; best_in_place is for "in place" editing of existing records, not creating new ones (for that, use collection_select in your _form partial for the new page).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文