About Me

My photo
जिंदगी की परीक्षा में कोई नम्बर नहीं मिलते है लोग आपको दिल से याद करे तो समझ लेना आप पास हो गए....

Wednesday 7 June 2017

How to add nested form in devise resource


How to add nested form in devise resource


Problem: Suppose We have a User has one Company model relationship (where company has name attribute) and while signup you want to add and validate company name (uniqueness).

Solution:

1.add following lines in User model

accepts_nested_attributes_for :company
validates_associated :company

2. To override devise views

rails g devise:views users 

3. Modify the app/views/devise/registrations/new.html.haml

  = form_for(resource, as: resource_name, url: registration_path(resource_name), class: "form-signin") do |f|
    %h4.form-signin-heading
      = "Sign up"
    = devise_error_messages!
    .field
      = f.email_field :email, autofocus: true, class: 'form-control', placeholder: "Email"
    = f.fields_for :company do |a|
      .field
        = a.text_field :name,  class: 'form-control', placeholder: "Company Name"
    .field
      = f.password_field :password, autocomplete: "off", class: 'form-control', placeholder: "Password"
    .field
      = f.password_field :password_confirmation, autocomplete: "off", class: 'form-control', placeholder: "Confirm Password"
    %br/
    %button.btn.btn-lg.btn-primary.btn-block{:type => "submit"} Sign Up
  = render "devise/shared/links"

5.   In Application Controller override devise's  configure_permitted_parameters method

def configure_permitted_parameters
  devise_parameter_sanitizer.permit(:sign_up, keys: [company_attributes: :name] )
end

6.Override Registration controller (generate Registration controller inside user folder) and copies the following co

class Users::RegistrationsController < Devise::RegistrationsController

  def new
    build_resource({})
    resource.build_company
    respond_with self.resource
  end
end

Please let me know if any issue or correction in my post.

Monday 5 June 2017

Vimuseful commands

1) :0 - move to first line (or 1 then shift+g ie. 1G means Go to line1 similarly 2G means 2nd line)
2) :$ - move to last line (or shift+g)
3) :q - quit a file 
4) Delete a char
    a) Get back to normal mode by pressing ESC
    b) Move the cursor over the char to be deleted and press x
5) move a line
   a) Get back to normal mode by pressing ESC
   b) Move the cursor over the line to be moved  Use dd to delete the line, then use p to paste it repeatedly into the document.

6)The commands to move one screen at a time are Ctrl-F (Forward) and Ctrl-B (Backward).
7)search:  which involves typing slash / followed by the string you want to find. The trick is to type /<string> followed by return, and then press n to go to the next match (if any).


Note: ls -hartl - useful for seeing which files and directories have recently changed

 human-readable values for the sizes (e.g., 29K instead of 29592 for a 29-kilobyte file), including all of them (even hidden ones),
ordered by reverse time,
long form.

Beautiful Lines : The classes are used mainly for styling, while the ids are included mainly to manipulate the Document Object Model (DOM) with JavaScript