J'essaie de comprendre les collections dans Rails. Je peux remplir la liste déroulante à partir d'une table de base de données, soumettre l'option sélectionnée et montrer le résultat. Cependant, je ne peux pas comprendre comment afficher l'option sélectionnée dans la liste déroulante lorsque l'utilisateur choisit de modifier l'entrée.Renvoyer la sélection d'origine dans collection_select lors de l'édition de l'entrée
Voici un extrait de mon code de la vue:
<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>
Voici mon code du contrôleur:
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
Et voici le code pour mes modèles:
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
Toute aide est grandement apprécié.
Merci!
S'il vous plaît montrer votre code vue ici – klew
Pouvez-vous ajouter votre modèle et le code du contrôleur à votre question? – klew
Je viens de l'ajouter Klew, merci pour votre aide. – Hinchy