Flog
Flay
Roodi
Rcov
Bonus FREE rails plugin
old_structure = [['username', 'bob'], ['password','secret']]
new_structure = {'username' => 'bob', 'password' => 'secret'}
new_structure = {}
old_structure.each do |element|
hash[element[0]] = element[1]
end
new_structure = old_structure.inject({}) do |results, element|
results[element[0]] = element[1]
results
end
new_structure = old_structure.inject({}) do |results, element|
results.merge( { element[0] => element[1] } )
end
Now, whenever the password in the object is modified, whether it is via a form submission or just updating the object in script/console, save the object and the password will be hashed before it is stored. If there are any validations that don't pass the object will not be saved and the password will be the same as it had been.
after_validation :hash_password
def hash_password
if self.password_changed?
self.password = Digest::MD5.hexdigest(self.password)
end
end
In this entry I am going to show how to, in a very search engine friendly way, update a URL for a particular page in a rails website. The rails routing system provides named routes, which are a very nice way to change the URL for a particular page and have all of your links update without having to change code in a bunch of different places. That is a great start on moving a page, but it doesn't take care of any links from outside of your site that might be pointing at the old URL. The best way to handle that is to make it so that the old URL redirects to the new URL, your end users won't notice a difference but the search engines bots will take note and update their indexes.
Lets pretend we are moving "http://example.com/users/signup" to "http://example.com/signup". Our original named route might look something like this:
map.signup '/users/signup', :controller => 'users', :action => 'signup'
<%= link_to 'Signup', signup_url %>
map.signup '/signup', :controller => 'users', :action => 'signup'
map.signup_old '/users/signup', :controller => 'users', :action => 'signup', :old_url => true
The lines in the route file would look like this:
before_filter :redirect_to_new_url
def redirect_to_new_url
if params[:old_url] == true
headers["Status"] = "301 Moved Permanently"
redirect_to({:action => action_name})
end
end
map.signup '/signup', :controller => 'users', :action => 'signup'
map.signup_old '/users/signup', :controller => 'users', :action => 'signup', :old_url => true