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?
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).
来源:https://stackoverflow.com/questions/5589652/rails-3-best-in-place-editing