class User < ActiveRecord::Base
  attr_accessor :password,:profile_attributes
  before_save :encrypt_new_password
  validates :email, uniqueness: {case_sensitive: false},  length: { in:
6..20 }, format: {with: /\A(\S+)@(.+)\.(\S+)\z/, message: "Formato de
correo no válido"}
  validates :password, confirmation: true, length: {within: 4..20},
presence: {if: :password_required?}
  validates :password_confirmation, presence: true

  has_one :profile, dependent: :destroy
  #Para poder hacer el formulario
  accepts_nested_attributes_for :profile, allow_destroy: true
end


class Profile < ActiveRecord::Base
  belongs_to :user
  belongs_to :role
  #validates :user_id, :role_id, presence: true
  has_attached_file :avatar, styles: { medium: '200x200>', thumb:
'48x48>' }
  validates_attachment_content_type :avatar, :content_type =>
/\Aimage\/.*\Z/

end

class UsersController < ApplicationController
  before_action :set_user, only: [:show, :edit, :update, :destroy]

  # GET /users
  # GET /users.json
  def index
    @users = User.all
  end

  # GET /users/1
  # GET /users/1.json
  def show
  end
  def show_profile
    @user = current_user
    @profile = Profile.find_by_id(current_user.profile.id)
  end

  # GET /users/new
  def new
    @user = User.new
    @user.build_profile
  end

  # GET /users/1/edit
  def edit

  end
  def cleanup string
    string.titleize
  end

  # POST /users
  # POST /users.json
  def create
    @user = User.new(user_params)
    if @user.valid?
      @role = Role.find_by(permission: 2)
      #@user.create_profile(role_id: @role.id, name:
cleanup(:profile_attributes[:name]))
      @user.create_profile(role_id: @role.id)
      respond_to do |format|
          if @user.save
            format.html { redirect_to @user, notice: 'Usuario creado
correctamente' }
            format.json { render :show, status: :created, location:
@user }

          else
            format.html { render :new }
            format.json { render json: @user.errors, status:
:unprocessable_entity }
          end
      end
    end
  end

  # PATCH/PUT /users/1
  # PATCH/PUT /users/1.json
  def update
    respond_to do |format|
      if @user.update(user_params)
        format.html { redirect_to @user, notice: 'User was successfully
updated.' }
        format.json { render :show, status: :ok, location: @user }
      else
        format.html { render :edit }
        format.json { render json: @user.errors, status:
:unprocessable_entity }
      end
    end
  end

  # DELETE /users/1
  # DELETE /users/1.json
  def destroy
    @user.destroy
    respond_to do |format|
      format.html { redirect_to users_url, notice: 'User was
successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
  # Use callbacks to share common setup or constraints between actions.
  def set_user
    @user = User.find_by_id(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white
list through.
  def user_params
    params.require(:user).permit(:user_name, :email, :password,
:password_confirmation, profile_attributes: [:id, :user_id, :role_id,
:name, :last_name, :avatar, :birthday])
  end
end




these are my files.....

-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/a1d0ab4e627375993af8faa574397f95%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to