Trainning Ruby Basics.
Trainning GIT.
Trainning MySQL.
Ruby on Rails Tutorial.

April 01, 2015

Omniauth With Facebook in Rails app - Simple demo

Posted by Unknown
0

Create a new rails application

# rails new sample_app

Open Gemfile and add following line into

gem 'omniauth'
gem 'omniauth-facebook'
Then run command to install it.
# bundle install

Create a provider

https://developers.facebook.com/

Go to My Apps and create a new app, copy App's ID and secret.

Configure

Configure your app to alive status

In config/initializers create omniauth.rb file

Add following lines
Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, 'YOUR_APP_ID', 'YOUR_APP_SECRET'
end

Create login page

# rails g controller sessions new create

Open config/routes.rb

get   '/login', :to => 'sessions#new' => :login
get '/auth/:provider/callback' => 'sessions#create'

Open your app/controllers/sessions_controller.rb file and write the create method, like this:

def create
  auth_hash = request.env['omniauth.auth']
 
  render :text => auth_hash.inspect
end
Then you open browser and point to http://localhost:3000/auth/facebook, you can see some info of your facebook account.

Create a user model

# rails g model User name:string email:string
# rails generate model Authorization provider:string uid:string user_id:integer

Add the following code to your app/models/user.rb file:

has_many :authorizations
validates :name, :email, :presence => true

Add the following code to your app/models/authorization.rb file:

belongs_to :user
validates :provider, :uid, :presence => true

Modified create function in sessions_controller.rb

def create
  auth_hash = request.env['omniauth.auth']
 
  @authorization = Authorization.find_by_provider_and_uid(auth_hash["provider"], auth_hash["uid"])
  if @authorization
    render :text => "Welcome back #{@authorization.user.name}! You have already signed up."
  else
    user = User.new :name => auth_hash["info"]["name"], :email => auth_hash["info"]["email"]
    user.authorizations.build :provider => auth_hash["provider"], :uid => auth_hash["uid"]
    user.save
 
    render :text => "Hi #{user.name}! You've signed up."
  end
end
Go back, refresh web app. You can see your name !

Create destroy function to logout

Open sessions_controller.rb and add function:
def destroy
  session[:user_id] = nil
  render :text => "You've logged out!"
end
Open routes.rb, add route:
get '/logout' => 'sessions#destroy'


Pretty URL in Rails 4 - Build a Simple Application

Posted by Unknown
0

Install gem stringex

Add following lines to Gemfile
gem 'stringex', '~> 2.5.2'
gem 'responders'
Run command
# bundle install

Rails application have model Post with id:integer and title:string

class Post < ActiveRecord::Base
  acts_as_url :title, url_attribute: :slug
 
  def to_param
    "#{id}-#{slug}"
  end
end

With config above, example: I have a post with id = 1 and title = "This is an example" then link to post look like

http://localhost:3000/posts/1-this-is-an-example



Nested Attributes in Rails 4 - Nested Model Form

Posted by Unknown
0

If you have models: Survey has many Questions and Question has many anwsers.

Your models

models/survey.rb

class Survey < ActiveRecord::Base
  has_many :questions, :dependent => :destroy
  accepts_nested_attributes_for :questions, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
end

models/question.rb

class Question < ActiveRecord::Base
  belongs_to :survey
  has_many :answers, :dependent => :destroy
  accepts_nested_attributes_for :answers, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
end

models/answer.rb

class Answer < ActiveRecord::Base
  belongs_to :question
end

Your controllers

survey_controller.rb

def new
  @survey = Survey.new
  3.times do
    question = @survey.questions.build
    4.times { question.answers.build }
  end
end
The new method above will create survey with 3 questions and 4 answer for each question.

create method in survey_controller.rb

  def create
    @survey = Survey.new survey_params
    if @survey.save
      flash[:success] = "New survey has been created!"
      redirect_to surveys_path
    else
      render 'new'
    end
  end

Strong params in rails 4

# in survey_controller.rb

private

def survey_params
  params.require(:survey).permit :name, questions_attributes: [:content, :name, answers_attributes: [:content]]
end
* Change these attributes to correct your app.

Your view files

views/surveys/new.html.erb

<%= form_for @survey do |f| %>
  <%= f.error_messages %>
  <%= f.label :name %>
  <%= f.text_field :name %>

  <%= f.fields_for :questions do |builder| %>
    <%= render "question_fields", :f => builder %>
  <% end %>
  <%= f.submit "Submit" %>

<% end %>

views/surveys/question_fields.html.erb

<%= f.label :content, "Question" %>

  <%= f.text_area :content, :rows => 3 %>

  <%= f.check_box :_destroy %>
  <%= f.label :_destroy, "Remove Question" %>



<%= f.fields_for :answers do |builder| %>
  <%= render 'answer_fields', :f => builder %>
<% end %>

views/surveys/answer_fields.html.erb

<%= f.label :content, "Answer" %>
  <%= f.text_field :content %>
  <%= f.check_box :_destroy %>
  <%= f.label :_destroy, "Remove" %>





Popular Posts

Labels

Archive

 

Blogroll

Recepies

Flickr Images

Copyright © 2014. Tutorials Blog - All Rights Reserved