About Me

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

Wednesday 12 July 2017

Internal Server Error (Rails Mina+passenger+ngnix ) rack-preloader.rb not found

Problem : when you  tail -200f /opt/nginx/logs/error.log 
Error msg:ruby: No such file or directory -- /home/ubuntu/.rvm/gems/ruby-2.3.1@<gemset-name>/gems/passenger-5.1.2/src/helper-scripts/rack-preloader.rb

Solution:
Login to production/staging server and got to app folder (verify gemset selected if using rvm)
just manually installed the same passenger version as

gem i passenger -v='5.1.2'

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  

Monday 22 May 2017

How to create symbolic link to folder

How to create symbolic link to folder

Suppose u have folder called testing_files (let's path /home/ravi/shared_folder/public/testing_files)

and

you want to make a symbolick under your project folder names as "assets" (let's path  /home/ravi/project_folder/public/assets)

           (source folder/actual folder)                                (symbolic link)
 ln -s /home/ravi/shared_folder/public/testing_files/   /home/ravi/project_folder/public/assets

be careful while giving symbolic link name assets folder should no be present or it will create a new one inside.
                                                                                

Saturday 30 April 2016

Useful sites

http://danknox.github.io/2013/01/27/using-rubys-native-nethttp-library/

Thursday 4 February 2016

Rails 4 way notes 1

1. Rails 4, generating a new application will result in the creation of binstubs for Rails executables, located
in the bin folder. A binstub is a script containing an executable that runs in the context of the bundle. This
means one does not have to prefix bundle exec each time a Rails specific executable is invoked. Binstubs are
also first class citizens in Rails 4, and should be added into your version control system like any other source
code file.
By default, the following stubs are available on every new Rails 4 project:
• bin/bundle
• bin/rails
• bin/rake

To add a binstub of a commonly used executable in your bundle, invoke bundle binstubs some-gem-name .
To illustrate, consider the following example:
$ bundle binstubs guard
which creates a binstub for guard in the bin folder.

2.Secret Token
Certain types of hacking involve modifying the contents of cookies without the server knowing about it.
By digitally signing all cookies sent to the browser, Rails can detect whether they were tampered with. The
secret_token.rb initializer contains the secret key base, randomly generated along with your app, which is
used to sign cookies.

p15
ref: rails 4 way

Ruby Learning Notes 1

.On Unix systems, you can use the “shebang” notation as the first line of the program file:#!/usr/bin/ruby
puts "Hello, Ruby Programmer"
puts "It is now #{Time.now}"

If you make this source file executable (using, for instance, chmod +x myprog.rb ), Unix lets you run the file as a program:

$ ./myprog.rb
Hello, Ruby Programmer
It is now 2013-05-27 12:30:36 -0500


If your system supports it, you can avoid hard-coding the path to Ruby in the “shebang” line by using
#!/usr/bin/env ruby , which will search your path for ruby and then execute it.




2.The ri tool is a local, command-line viewer for ruby documentation. Most Ruby distributions now also install the resources used by the ri program.
To find the documentation for a class, type ri ClassName. For example, the following is the summary information for the GC class. (To get a list of classes with ri documentation, type
ri with no arguments.)
$ ri GC

If you installed Ruby using rvm, there’s one additional step to get ri documentation available. At a
prompt, enter rvm docs generate .

3.Class A class is a combination of state and methods that use that state.

Naming Convention:
Local variables, method parameters, and method names should all start with a lowercase letter or an underscore. Global variables are prefixed with a dollar
sign ($), and instance variables begin with an “at” sign (@). Class variables start with two “at” signs (@@). Finally, class names, module names, and constants must start with an uppercase letter.

Hash: a hash by default returns nil when indexed by a key it doesn’t
contain.specifying a default value(which is by default nil) when you create a new, empty hash.
histogram = Hash.new(0)   # This overrides default value from nil to zero
histogram['ruby'] # => 0

Symbol: Symbols are simply constant names that you don’t have to  predeclare and that are guaranteed to be unique. A symbol literal starts with a colon and is
normally followed by some kind of name

Ruby statement modifiers are a useful shortcut if the body of an if or while statement is just a single expression.

if radiation > 3000
  puts "Danger, Will Robinson"
end

Here it is again, rewritten using a statement modifier:
puts "Danger, Will Robinson" if radiation > 3000


Regular expression: create a regular expression by writing a pattern between
slash characters (/pattern/)

We can put all this together to produce some useful regular expressions:
/\d\d:\d\d:\d\d/   # a time such as 12:34:56
/Perl.*Python/  #Perl, zero or more other chars, then Python
/Perl Python/    #Perl, a space, and Python
/Perl *Python/  #Perl, zero or more spaces, and Python
/Perl +Python/  #Perl, one or more spaces, and Python
/Perl\s+Python/  #Perl, whitespace characters, then Python
/Ruby (Perl|Python)/   #Ruby, a space, and either Perl or Python

The match operator =~ can be used to match a string against a regular expression. If the pattern is found in the string,=~ returns its starting position; otherwise, it returns nil . This means you can use regular expressions as the condition in if and while statements.

code blocks: which are chunks of code you can associate with method invocations, almost as if they were parameters.Code blocks are just chunks of code between braces or between do and end . This is a code block:
{ puts "Hello" }

a Ruby standard and use braces for single-line blocks and do / end for multiline blocks.

ARGV :When you run a Ruby program from the command line, you can pass in arguments. the array ARGV contains each of the arguments passed to the running program. Create a file called cmd_line.rb that contains the following:
puts "You gave #{ARGV.size} arguments"
p ARGV

When we run it with arguments, we can see that they get passed in:
$ ruby cmd_line.rb ant bee cat dog
You gave 4 arguments
["ant", "bee", "cat", "dog"]


The p method prints out an internal representation of an object.
b1 = BookInStock.new("isbn1", 3)
p b1

produces:
#<BookInStock:0x007fac4910f3e0 @isbn="isbn1", @price=3.0>

To read the instance variable of object out side the world we wrote attribute accessor method , but in ruby we need not to write for each instance variable since ruby provides shortcut for that "attr_reader", as example

class BookInStock

  attr_reader :isbn,:price
 def initialize(isbn,price)
   @isbn = isbn
   @price = price
 end
end

book = BookInStock.new("isbn1", 12.34)
puts "ISBN = #{book.isbn}"
puts "Price = #{book.price}"





~                             
Ruby provides a shortcut for creating these simple attribute-setting methods. If you want a write-only accessor, you can use the form attr_writer , but that’s fairly rare. You’re far more likely to want both a reader and a writer for a given attribute, so you’ll use the handy-dandy attr_accessor method:

pg36?(floating point)


References:

Programming Ruby: The Pragmatic Programmer's Guide