Marten Oosterhoff
2005-Oct-21 13:37 UTC
Newbie: How to update parents from child-controller
Hi, I''ve a problem with parents ;-) specially updating them from child-controllers. Updating "downwards" works, eg. Alumni updates her Subjects, but now I''m struggling with updating/deleting the parent-model(user) for alumnis and lecturers. Let''s say an alumni wants to update his or her email-address. Any hint''s where I can find more infos about that? I''m willing on writing an Howto for that problem. Do I have to build a new method in the myaccount controller, or is it better to modify the create-method in the alumnis-controller? --8<--------------------- snip 8<----------------------------- users (login,pwd,name,...,email,usertype[default:nil]) has_many :alumnis has_many :lecturers lecturers (user_id,...) belongs_to :user has_many :subjects subjects (lecturer_id,title) belongs_to :lecturer habtm :alumnis alumnis (user_id,job,...) belongs_to :user habtm :subjects alumnis_subjects (alumni_id,lecturer_id) --8<--------------------- snip 8<----------------------------- class AlumnisController < ApplicationController def edit @alumni = Alumni.find(params[:id]) @subjects = Subject.find_all # view uses checkboxes, marks where alumnis_subjects end def update @alumni = Alumni.find(params[:id]) # updating alumni_subjects -n2G8xh6EFerYdqCpXKCAvQ@public.gmane.org = Subject.find(@params[:subject_ids]) if @params[:subject_ids] if @alumni.update_attributes(params[:alumni]) flash[:notice] = ''Alumni was successfully updated.'' redirect_to :action => ''list''#, :id => @alumni else render :action => ''edit'' end end end --8<--------------------- snip 8<----------------------------- Thanks for your help - Marten
Steve Prentice
2005-Oct-21 21:18 UTC
Re: Newbie: How to update parents from child-controller
def update @alumni = Alumni.find(@params[:id]) @alumni.user.email = @params[:email]) end On Fri, 2005-10-21 at 15:37 +0200, Marten Oosterhoff wrote:> Hi, > > I''ve a problem with parents ;-) specially updating them from child-controllers. > > Updating "downwards" works, eg. Alumni updates her Subjects, but now > I''m struggling with updating/deleting the parent-model(user) for > alumnis and lecturers. > > Let''s say an alumni wants to update his or her email-address. Any > hint''s where I can find more infos about that? > > I''m willing on writing an Howto for that problem. > > Do I have to build a new method in the myaccount controller, or is it > better to modify the create-method in the alumnis-controller? > > --8<--------------------- snip 8<----------------------------- > > users (login,pwd,name,...,email,usertype[default:nil]) > has_many :alumnis > has_many :lecturers > > lecturers (user_id,...) > belongs_to :user > has_many :subjects > > subjects (lecturer_id,title) > belongs_to :lecturer > habtm :alumnis > > alumnis (user_id,job,...) > belongs_to :user > habtm :subjects > > alumnis_subjects (alumni_id,lecturer_id) > > --8<--------------------- snip 8<----------------------------- > > class AlumnisController < ApplicationController > def edit > @alumni = Alumni.find(params[:id]) > @subjects = Subject.find_all # view uses checkboxes, marks where > alumnis_subjects > end > > def update > @alumni = Alumni.find(params[:id]) > # updating alumni_subjects > @alumni.subjects = Subject.find(@params[:subject_ids]) if > @params[:subject_ids] > if @alumni.update_attributes(params[:alumni]) > flash[:notice] = ''Alumni was successfully updated.'' > redirect_to :action => ''list''#, :id => @alumni > else > render :action => ''edit'' > end > end > end > > --8<--------------------- snip 8<----------------------------- > > Thanks for your help > - Marten > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Marten Oosterhoff
2005-Nov-02 16:02 UTC
Re: Newbie: How to update parents from child-controller
2005/10/21, Steve Prentice <prentice-p8dDKEW+AByWw5T4fKrT70EOCMrvLtNR@public.gmane.org>:> def update > @alumni = Alumni.find(@params[:id]) > @alumni.user.email = @params[:email]) > endSteve, thanks for your answer and sorry for replying that late. Unfortunately your solution doesn''t work for me. Are you sure that there is nothing more to do , e.g. with update_attributes ? if @alumni.user.update_attributes(params[:user]) is not working for me Regards - Marten
Marten Oosterhoff
2005-Nov-02 17:50 UTC
Re: Newbie: How to update parents from child-controller
> http://wiki.rubyonrails.com/rails/pages/CheckboxHABTM > If it does not help you, then send us your view code snippet related to > alumni update.Reposting my code-snippets from Oct. 21st. Thanks for your help. --8<--------------------- snip 8<----------------------------- users (login,pwd,name,...,email,usertype[default:nil]) has_many :alumnis has_many :lecturers lecturers (user_id,...) belongs_to :user has_many :subjects subjects (lecturer_id,title) belongs_to :lecturer habtm :alumnis alumnis (user_id,job,...) belongs_to :user habtm :subjects alumnis_subjects (alumni_id,lecturer_id) --8<--------------------- snip 8<----------------------------- class AlumnisController < ApplicationController def edit @alumni = Alumni.find(params[:id]) @subjects = Subject.find_all # view uses checkboxes, marks where alumnis_subjects end def update @alumni = Alumni.find(params[:id]) # updating alumni_subjects @alumni.subjects = Subject.find(@params[:subject_ids]) if @params[:subject_ids] if @alumni.update_attributes(params[:alumni]) flash[:notice] = ''Alumni was successfully updated.'' redirect_to :action => ''list''#, :id => @alumni else render :action => ''edit'' end end end --8<--------------------- snip 8<-----------------------------
Marten Oosterhoff wrote:> 2005/10/21, Steve Prentice <prentice-p8dDKEW+AByWw5T4fKrT70EOCMrvLtNR@public.gmane.org>: > >>def update >> @alumni = Alumni.find(@params[:id]) >> @alumni.user.email = @params[:email]) >>end > > > Steve, thanks for your answer and sorry for replying that late. > Unfortunately your solution doesn''t work for me. Are you sure that > there is nothing more to do , e.g. with update_attributes ? > > if @alumni.user.update_attributes(params[:user]) is not working for me > > Regards > - Marten > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >Marten, you should use the update_attributes method in the update action. Please check this page: http://wiki.rubyonrails.com/rails/pages/CheckboxHABTM If it does not help you, then send us your view code snippet related to alumni update. Eszter -- Primalgrasp LLC http://primalgrasp.com
Marten Oosterhoff
2005-Nov-02 23:04 UTC
Re: Newbie: How to update parents from child-controller
Actually I think I found the problem: I''m using the LoginController and if I want to update the user the model throws errors ---------------------------- errors: !ruby/object:ActiveRecord::Errors base: *id002 errors: password: - "can''t be blank" - is too short (min is 5 characters) ---------------------------- But as I understand the code the model checks, if the password is blank, speak I don''t wanna change it, an so it updates it with the "old" password. ---------------------------- before_update :crypt_unless_empty # If the record is updated we will check if the password is empty. # If its empty we assume that the user didn''t want to change his # password and just reset it to the old value. def crypt_unless_empty if password.empty? user = self.class.find(self.id) self.password = user.password else write_attribute "password", self.class.sha1(password) end end validates_uniqueness_of :login, :on => :create validates_confirmation_of :password validates_length_of :login, :within => 3..40 validates_length_of :password, :within => 5..40 validates_presence_of :login, :password, :password_confirmation -------------------------- Why does updating, e.g. the email not work?