Never been to DZone Snippets before?

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

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

Linking to the Current Action

This small snippet shows how to link to the current action. Although it's very easy to do, it doesn't seem to be documented anywhere. This comes in handy when your dealing with partials that are called in multiple actions and controllers.
   1  
   2  <%= link_to "Insert Link Text", :action => controller.action_name %>

.htaccess That Fixes The Trailing Slash Error

// This .htaccess file is for use with a Rails application that is accessed by a symbolic link. This fixes an error in which the directory URL must have a trailing slash. Otherwise, the user receives a 400 Bad Request error.

   1  
   2  Options +FollowSymLinks +ExecCGI
   3  
   4  RewriteEngine On
   5  
   6  RewriteCond %{SCRIPT_FILENAME}    -d
   7  RewriteCond %{SCRIPT_FILENAME}      ^.*[^\/]$
   8  RewriteRule ^(.*)$ $1/ [N]
   9  
  10  #Put the directory your Rails app is in here.
  11  RewriteBase /directory
  12  
  13  RewriteRule ^$ index.html [QSA]
  14  RewriteRule ^([^.]+)$ $1.html [QSA]
  15  
  16  RewriteCond %{REQUEST_FILENAME} !-f
  17  RewriteRule ^(.*)$ dispatch.cgi?$1 [QSA,L]

Creating A Two Dimensional Table with Rails

This code creates a two dimensional table out of data passed from the controller. The code is pretty basic, but there's not much documentation on it.

   1  
   2  <table>
   3  <% @instance.in_groups_of(5) do |row| %>
   4  <tr> 
   5  <% row.each do |data| %>
   6          # The nil check is required because Rails pads empty table cells with nil.
   7  	<td><%= data.field unless data.nil? %></td>
   8  <% end %>
   9  </tr> 
  10  <% end %>
  11  </table>

Quick and Dirty Rails Code

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:

   1  
   2    def updatesettings
   3      # This method is probably one of the most messiest things I have ever written.
   4      # It's a piece of garbage and an example of how NOT to code something like this.
   5      @settings = User.find(params[:id])
   6      @settings.attributes =  params[:settings]
   7      if request.post?
   8        oldpassword = @settings.password
   9        @settings.login = params[:settings][:login]
  10        @settings.email = params[:settings][:email]
  11        if params[:password][:newpassword1] != "" || params[:password][:newpassword2] != ""
  12          if params[:password][:newpassword1] == params[:password][:newpassword2]
  13            @settings.password = Digest::SHA1.hexdigest("FAKE CODE HERE--#{params[:password][:newpassword1]}--")
  14          else
  15            flash[:sucess] = "Your new passwords don't match."
  16            redirect = "settings"
  17          end 
  18        end
  19        @settings.save
  20        if params[:id] != nil
  21          if redirect == nil && "#{params[:id]}" != "#{session[:user_id]}"
  22            flash[:sucess] = "User has been sucessfully updated."
  23            redirect_to :controller => "users"
  24          else
  25            redirect_to :action => "index", :id => params[:id]
  26          end
  27        else
  28          flash[:sucess] = "Your settings have been updated."
  29          redirect_to :action => "users"
  30        end
  31      end
  32    end


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

   1  
   2  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.
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS