I'm trying to get my head around collection_selects in Rails. I can populate the dropdown from a database table, submit the selected option, and show the result. However I can't figure out how to show the selected option in the dropdown when the开发者_如何学JAVA user chooses to edit the entry.
Here's an extract from my view code:
<p>
<%= f.label :Status %><br />
<%= f.text_field :status %>
</p>
<p>
<%= f.label :Manager %><br />
<%= f.collection_select(:manager, @managers, :id, :name, {:include_blank => true}) %>
</p>
Here's my controller code:
class ProjectsController < ApplicationController
# GET /projects
# GET /projects.xml
before_filter :require_user
def index
@projects = Project.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @projects }
end
end
# GET /projects/1
# GET /projects/1.xml
def show
@project = Project.find(params[:id])
projectid = params[:id]
@evidence = Evidence.find(:all, :conditions => ["projectid = ?", projectid])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @project }
end
end
# GET /projects/new
# GET /projects/new.xml
def new
@project = Project.new
@managers = Manager.find(:all)
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @project }
end
end
# GET /projects/1/edit
def edit
@project = Project.find(params[:id])
@managers = Manager.find(:all)
end
# POST /projects
# POST /projects.xml
def create
@project = Project.new(params[:project])
respond_to do |format|
if @project.save
flash[:notice] = 'Project was successfully created.'
format.html { redirect_to(@project) }
format.xml { render :xml => @project, :status => :created, :location => @project }
else
format.html { render :action => "new" }
format.xml { render :xml => @project.errors, :status => :unprocessable_entity }
end
end
end
# PUT /projects/1
# PUT /projects/1.xml
def update
@project = Project.find(params[:id])
respond_to do |format|
if @project.update_attributes(params[:project])
flash[:notice] = 'Project was successfully updated.'
format.html { redirect_to(@project) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @project.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /projects/1
# DELETE /projects/1.xml
def destroy
@project = Project.find(params[:id])
@project.destroy
respond_to do |format|
format.html { redirect_to(projects_url) }
format.xml { head :ok }
end
end
end
And here's the code for my models:
class Manager < ActiveRecord::Base
has_many :projects
end
class Project < ActiveRecord::Base
belongs_to :manager
validates_presence_of(:name, :reference, :client, :status)
validates_uniqueness_of (:reference)
end
Any help is greatly appreciated.
Thanks!
According to the documentation:
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
...The value returned from calling method on the instance object will be selected.
This means that your :manager
method must return a value that matches one of the managers in your @managers
instance variable in order for that to be the selected option.
I think you need to change :manager
to :manager_id
:
<%= f.collection_select(:manager_id, @managers, :id, :name, {:include_blank => true}) %>
精彩评论