DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Quick And Dirty Rails Code

  • submit to reddit
        This is one of the methods in a small CMS I'm writing, I thought I'd post here since it's just a mess. Take a look at the obfuscation:

  def updatesettings
    # This method is probably one of the most messiest things I have ever written.
    # It's a piece of garbage and an example of how NOT to code something like this.
    @settings = User.find(params[:id])
    @settings.attributes =  params[:settings]
      oldpassword = @settings.password
      @settings.login = params[:settings][:login] = params[:settings][:email]
      if params[:password][:newpassword1] != "" || params[:password][:newpassword2] != ""
        if params[:password][:newpassword1] == params[:password][:newpassword2]
          @settings.password = Digest::SHA1.hexdigest("FAKE CODE HERE--#{params[:password][:newpassword1]}--")
          flash[:sucess] = "Your new passwords don't match."
          redirect = "settings"
      if params[:id] != nil
        if redirect == nil && "#{params[:id]}" != "#{session[:user_id]}"
          flash[:sucess] = "User has been sucessfully updated."
          redirect_to :controller => "users"
          redirect_to :action => "index", :id => params[:id]
        flash[:sucess] = "Your settings have been updated."
        redirect_to :action => "users"

edit: I fixed it up so all of the code is in the model now.

settings = @settings.updatesettings(params[:id], params[:settings][:login], params[:settings][:email], params[:password][:newpassword1], params[:password][:newpassword2])

I'd say that's a little bit better.    


fdfdfd dfdf replied on Fri, 2009/05/22 - 2:55am

The obfuscation was nice but needed much info to go through it.I really had a interest on this topic. Car Accident Compensation Claims

fgfgf dfdfd replied on Tue, 2009/05/19 - 11:08pm

How to change these codings.I have no idea of this.please provide some more details. Russia Train Tickets

drage sheter replied on Tue, 2009/04/28 - 7:36am

Hey I tried but I could not understood the coding.Can you tell that which language coding is this-mentioned.facia plate

Snippets Manager replied on Fri, 2006/10/20 - 1:36am

fifoo: Well, like NoKarma said, all of the code interacting with the database should be in a model, not a controller. The redirects are also quite confusing for someone that doesn't know what's going on in the code, they can probably be done in a much better fashion.

Snippets Manager replied on Tue, 2006/08/15 - 6:37pm

Do not put Model specific code into the controller (like the password validation and the password hashing and all the other stuff...).

Snippets Manager replied on Fri, 2006/10/20 - 7:29am

Hi, How would you write it the Ruby-way then? Thanks!