Skip to content

Instantly share code, notes, and snippets.

@kvirani
Forked from anonymous/controller.rb
Created March 26, 2012 19:00
Show Gist options
  • Save kvirani/2208716 to your computer and use it in GitHub Desktop.
Save kvirani/2208716 to your computer and use it in GitHub Desktop.
# The controller method for the same is as below
def update_additional_details
@user = current_user
@user.profile.display_education_details(show_details_for_profile?(:education))
@user.profile.display_experience_details(show_details_for_profile?(:experience))
@user.profile.display_linkedin_details(show_details_for_profile?(:linkedin_profile))
@user.profile.detail[:linkedin_profile][:data] = params[:linkedin_profile_data]
if @user.profile.save
redirect_to additional_details_user_path(@user), :notice => _("successfully_updated")
else
# there are errors, do not redirect the user, just render the form (template) they just submitted
render 'edit' # or whatever template they were just looking at
end
end
class Profile < ActiveRecord::Base
belongs_to :user
# SERIALIZED ATTRIBUTES
serialize :detail, Hash
# Serialized Preferences column - uses UserPreference class.
serialize :preferences, ProfilePreference
# VALIDATIONS
validate :url_valid, :on => :update
def url_valid
url = URI.parse(get_linkedin_profile)
unless %w( http https ).include?(url.scheme) && (url.hostname.include? "linkedin") && !url.path.blank?
errors.add(:detail, "Please Enter a Valid Linkedin Profile URL")
end
rescue URI::InvalidURIError => e
errors.add(:detail, "Please Enter a Valid Linkedin Profile URL")
end
end
=begin
In the view I have something like this
= simple_form_for(@user, :url => update_additional_details_user_path(@user), ) do |f|
fieldset
= render "shared/error_messages", :target => @user
Its actually user.profile so I am passing user object and shared/error_messages looks like this :
- if target.errors.any?
#errorExplanation.alert.alert-error.inline_notification
h5 = _("activerecord.errors.template.header", :model => target.class.model_name.human, :count => target.errors.size)
ul.unstyled
- target.errors.full_messages.each do |msg|
li = msg
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment