Skip to content

Instantly share code, notes, and snippets.

@aj07mm
Last active February 14, 2016 15:42
Show Gist options
  • Save aj07mm/d9374c23d3e338965d09 to your computer and use it in GitHub Desktop.
Save aj07mm/d9374c23d3e338965d09 to your computer and use it in GitHub Desktop.
Tudo que vc precisa saber sobre rails

Rails shit:

rails new myapp --database=postgresql
rails generate foundation:install
rails generate devise_install
rails generate controller home index
rails generate migration create_relationship_table
rails destroy controller lalala
rails destroy model yadayada
rails destroy scaffold hohoho // After destroying, rake db:rollback!

Relationship:

  • many to many rails g scaffold contact name:string email:string rails g scaffold type name:string contact_id:integer contacts:references

Remove Turbolinks to use javascript

Remove the gem 'turbolinks' line from your Gemfile. 
Remove the require turbolinks from your app/assets/javascripts/application.js.
Remove the two "data-turbolinks-track" => true hash key/value pairs from your app/views/layouts/application.html.erb.

http://corlewsolutions.com/articles/article-9-remove-uninstall-delete-turbolinks-in-rails-4

Migrations (last step):

rake db:setup
rake db:rollback
rake db:rollback STEP=1
rake db:rollback STEP=n
rake db:purge
rake db:drop 

adding column to table

add_column :users, :email, :string  

Dropdown

<%= select("contact_id", "type_id", @type.collect {|r| [ r.name, r.id ] }, { :include_blank => true }) %>

or

<%= f.label :type %>
<%= select(@field, "type", Field::types, {include_blank: false}) %>

Helpers

Só definir no arquivo em usar na view o metodo

<%= text_field_tag(:q) %>
<%= text_area_tag(:message, "Hi, nice site", size: "24x6") %>
<%= password_field_tag(:password) %>
<%= hidden_field_tag(:parent_id, "5") %>
<%= search_field(:user, :name) %>
<%= telephone_field(:user, :phone) %>
<%= date_field(:user, :born_on) %>
<%= datetime_field(:user, :meeting_time) %>
<%= datetime_local_field(:user, :graduation_day) %>
<%= month_field(:user, :birthday_month) %>
<%= week_field(:user, :birthday_week) %>
<%= url_field(:user, :homepage) %>
<%= email_field(:user, :address) %>
<%= color_field(:user, :favorite_color) %>
<%= time_field(:task, :started_at) %>
<%= number_field(:product, :price, in: 1.0..20.0, step: 0.5) %>
<%= range_field(:product, :discount, in: 1..100) %>

Heroku Shit

heroku keys:add #upload id_rsa.pub
heroku run rake
heroku rake db:migrate
heroku pg:psql --app hemicroblog HEROKU_POSTGRESQL_MAUVE

heroku pg:reset #reset database
heroku run rake db:purge #reset database
A pasta test é a default para testes

Rspec:

rails generate rspec:install
rspec spec/helpers/follow_helper_spec.rb 

##Gem install

add to Gemfile:

gem 'devise'

then:

bundle install

Devise:

rails generate devise:install
rails generate devise MODEL #create ifnotexists

set mailler, on 'config/environments/development.rb', add:

config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

add on MODEL:

before_action :authenticate_user!

verify user signed:

user_signed_in?

get current user:

current_user

get user session:

user_session

adding routes:

devise_for :users

devise_scope :user do
    get "sign_in", to: "devise/sessions#new"
    get "sign_out", to: "devise/sessions#destroy"
end

more on: https://github.com/plataformatec/devise

add to routes:

#create a 'localhost:3000/' route

root 'home#index' 

###### views from the controller 'home'

get 'home/index'
get 'home/about'

OBS: first: 

rails g controller home

h
then add the view.erb files in a home folder

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment