gpt4 book ai didi

ruby-on-rails - 编辑条目时返回 collection_select 中的原始选择

转载 作者:行者123 更新时间:2023-12-05 01:13:37 24 4
gpt4 key购买 nike

我正在努力了解 Rails 中的 collection_selects。我可以从数据库表中填充下拉列表,提交选定的选项,然后显示结果。但是,当用户选择编辑条目时,我不知道如何在下拉列表中显示所选选项。

这是我的 View 代码的摘录:

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

这是我的 Controller 代码:

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

这是我的模型的代码:

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

非常感谢任何帮助。

谢谢!

最佳答案

根据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.

这意味着您的 :manager 方法必须返回一个与您的 @managers 实例变量中的其中一位经理相匹配的值,以便它成为选定的选项。

关于ruby-on-rails - 编辑条目时返回 collection_select 中的原始选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2325358/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com