I''m currently doing all of this just to have a drop-down list of Courses for a particular student: ===== edit template ==== <%= f.select :course, @courses, :selected => @student.course.id %> ================== ===== students controller ==== def edit @student = Student.find(params[:id]) @page_title = "Edit #{@student.full_name}" @courses = Course.find(:all).collect { |c| [ c.name, c.id ] } end def update @student = Student.find(params[:id]) @student.attributes = params[:student] @student.course = Course.find(params[:student][:course]) respond_to do |format| if @student.save flash[:notice] = "#{@student.full_name} was successfully updated." format.html { redirect_to students_url } format.xml { head :ok } else format.html { render :action => "edit" } format.xml { render :xml => @student.errors.to_xml } end end end =================== Students :belongs_to :course, and Course :has_many :students. I''ve tried several different ways of doing this, and this has been the first that''s actually successful. Having already tried the bad way, I''d like to think this is the ugly way, and hope someone has a suggestion on the Good Way? :) Any help would be appreciated. Thanks! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
How about: ===== edit template ==== <%= f.select :course_id, @courses %> ================== ===== students controller ==== def edit @student = Student.find(params[:id]) @page_title = "Edit #{@student.full_name}" @courses = Course.find(:all).collect { |c| [ c.name, c.id ] } end def update @student = Student.find(params[:id]) @student.attributes = params[:student] respond_to do |format| if @student.save flash[:notice] = "#{@student.full_name} was successfully updated." format.html { redirect_to students_url } format.xml { head :ok } else format.html { render :action => "edit" } format.xml { render :xml => @student.errors.to_xml } end end end =================== On Dec 2, 3:24 am, Doug <dougtmayer-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m currently doing all of this just to have a drop-down list of > Courses for a particular student: > > ===== edit template ====> <%= f.select :course, @courses, :selected => @student.course.id %> > ==================> > ===== students controller ====> def edit > @student = Student.find(params[:id]) > @page_title = "Edit #{@student.full_name}" > @courses = Course.find(:all).collect { |c| [ c.name, c.id ] } > end > > def update > @student = Student.find(params[:id]) > @student.attributes = params[:student] > @student.course = Course.find(params[:student][:course]) > > respond_to do |format| > if @student.save > flash[:notice] = "#{@student.full_name} was successfully > updated." > format.html { redirect_to students_url } > format.xml { head :ok } > else > format.html { render :action => "edit" } > format.xml { render :xml => @student.errors.to_xml } > end > end > end > ===================> > Students :belongs_to :course, and Course :has_many :students. I''ve > tried several different ways of doing this, and this has been the > first that''s actually successful. Having already tried the bad way, > I''d like to think this is the ugly way, and hope someone has a > suggestion on the Good Way? :) Any help would be appreciated. > > Thanks!--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
When I get that, I get an exception: Course expected, but got String On Dec 2, 9:41 am, Nicholas Henry <nicholas.he...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> How about: > > ===== edit template ====> <%= f.select :course_id, @courses %> > ==================> > ===== students controller ====> def edit > @student = Student.find(params[:id]) > @page_title = "Edit #...@student.full_name}" > @courses = Course.find(:all).collect { |c| [ c.name, c.id ] } > end > > def update > @student = Student.find(params[:id]) > @student.attributes = params[:student] > > respond_to do |format| > if @student.save > flash[:notice] = "#...@student.full_name} was successfully > updated." > format.html { redirect_to students_url } > format.xml { head :ok } > else > format.html { render :action => "edit" } > format.xml { render :xml => @student.errors.to_xml } > end > end > end > ===================> > On Dec 2, 3:24 am, Doug <dougtma...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I''m currently doing all of this just to have a drop-down list of > > Courses for a particular student: > > > ===== edit template ====> > <%= f.select :course, @courses, :selected => @student.course.id %> > > ==================> > > ===== students controller ====> > def edit > > @student = Student.find(params[:id]) > > @page_title = "Edit #...@student.full_name}" > > @courses = Course.find(:all).collect { |c| [ c.name, c.id ] } > > end > > > def update > > @student = Student.find(params[:id]) > > @student.attributes = params[:student] > > @student.course = Course.find(params[:student][:course]) > > > respond_to do |format| > > if @student.save > > flash[:notice] = "#...@student.full_name} was successfully > > updated." > > format.html { redirect_to students_url } > > format.xml { head :ok } > > else > > format.html { render :action => "edit" } > > format.xml { render :xml => @student.errors.to_xml } > > end > > end > > end > > ===================> > > Students :belongs_to :course, and Course :has_many :students. I''ve > > tried several different ways of doing this, and this has been the > > first that''s actually successful. Having already tried the bad way, > > I''d like to think this is the ugly way, and hope someone has a > > suggestion on the Good Way? :) Any help would be appreciated. > > > Thanks!--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Oops.. forgot to mark attr_accessible :course_id. :) Doing what you suggested works as expected. I tried a lot of different combinations to get what I wanted, I just must have missed this one. Thanks! On Dec 2, 9:41 am, Nicholas Henry <nicholas.he...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> How about: > > ===== edit template ====> <%= f.select :course_id, @courses %> > ==================> > ===== students controller ====> def edit > @student = Student.find(params[:id]) > @page_title = "Edit #...@student.full_name}" > @courses = Course.find(:all).collect { |c| [ c.name, c.id ] } > end > > def update > @student = Student.find(params[:id]) > @student.attributes = params[:student] > > respond_to do |format| > if @student.save > flash[:notice] = "#...@student.full_name} was successfully > updated." > format.html { redirect_to students_url } > format.xml { head :ok } > else > format.html { render :action => "edit" } > format.xml { render :xml => @student.errors.to_xml } > end > end > end > ===================> > On Dec 2, 3:24 am, Doug <dougtma...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I''m currently doing all of this just to have a drop-down list of > > Courses for a particular student: > > > ===== edit template ====> > <%= f.select :course, @courses, :selected => @student.course.id %> > > ==================> > > ===== students controller ====> > def edit > > @student = Student.find(params[:id]) > > @page_title = "Edit #...@student.full_name}" > > @courses = Course.find(:all).collect { |c| [ c.name, c.id ] } > > end > > > def update > > @student = Student.find(params[:id]) > > @student.attributes = params[:student] > > @student.course = Course.find(params[:student][:course]) > > > respond_to do |format| > > if @student.save > > flash[:notice] = "#...@student.full_name} was successfully > > updated." > > format.html { redirect_to students_url } > > format.xml { head :ok } > > else > > format.html { render :action => "edit" } > > format.xml { render :xml => @student.errors.to_xml } > > end > > end > > end > > ===================> > > Students :belongs_to :course, and Course :has_many :students. I''ve > > tried several different ways of doing this, and this has been the > > first that''s actually successful. Having already tried the bad way, > > I''d like to think this is the ugly way, and hope someone has a > > suggestion on the Good Way? :) Any help would be appreciated. > > > Thanks!--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---