homes_controller.rb
===============
class HomesController < ApplicationController
before_action :set_home, only: [:show, :edit, :update, :destroy]
# GET /homes
# GET /homes.json
def index
@homes = Home.all
end
# GET /homes/1
# GET /homes/1.json
def show
end
# GET /homes/new
def new
@home = Home.new
end
# GET /homes/1/edit
def edit
end
def verify
result = Home.where(email: params[:homes][:email]).first
if result
@message = "Email exist"
else
@message = "Email doesn't exist"
end
end
# POST /homes
# POST /homes.json
def create
@home = Home.new(home_params)
respond_to do |format|
if @home.save
format.html { redirect_to @home, notice: 'Home was successfully created.' }
format.json { render :show, status: :created, location: @home }
else
format.html { render :new }
format.json { render json: @home.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /homes/1
# PATCH/PUT /homes/1.json
def update
respond_to do |format|
if @home.update(home_params)
format.html { redirect_to @home, notice: 'Home was successfully updated.' }
format.json { render :show, status: :ok, location: @home }
else
format.html { render :edit }
format.json { render json: @home.errors, status: :unprocessable_entity }
end
end
end
# DELETE /homes/1
# DELETE /homes/1.json
def destroy
@home.destroy
respond_to do |format|
format.html { redirect_to homes_url, notice: 'Home was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_home
@home = Home.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def home_params
params.require(:home).permit(:email)
end
end
homes/_form.html.erb
==============
<%= form_for(home) do |f| %>
<% if home.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(home.errors.count, "error") %> prohibited this home from being saved:</h2>
<ul>
<% home.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :email %>
<%= f.text_field :email, id: "email" %>
<span id ="error-span"></span>
</div>
<button type="button" id="btns" required="true">verify</button>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<script>
$('#btns').on('click', function(){
var email = $('#email').val();
$.ajax({
type:'POST',
url: '/homes/verify',
data: $.param({ homes: {email: email }})
});
})
</script>
verify.js.erb
==============
$('#error-span').html('<%= @message %>');
routes.rb
============
Rails.application.routes.draw do
resources :homes
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
post '/homes/verify' => 'homes#verify'
end
===============
class HomesController < ApplicationController
before_action :set_home, only: [:show, :edit, :update, :destroy]
# GET /homes
# GET /homes.json
def index
@homes = Home.all
end
# GET /homes/1
# GET /homes/1.json
def show
end
# GET /homes/new
def new
@home = Home.new
end
# GET /homes/1/edit
def edit
end
def verify
result = Home.where(email: params[:homes][:email]).first
if result
@message = "Email exist"
else
@message = "Email doesn't exist"
end
end
# POST /homes
# POST /homes.json
def create
@home = Home.new(home_params)
respond_to do |format|
if @home.save
format.html { redirect_to @home, notice: 'Home was successfully created.' }
format.json { render :show, status: :created, location: @home }
else
format.html { render :new }
format.json { render json: @home.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /homes/1
# PATCH/PUT /homes/1.json
def update
respond_to do |format|
if @home.update(home_params)
format.html { redirect_to @home, notice: 'Home was successfully updated.' }
format.json { render :show, status: :ok, location: @home }
else
format.html { render :edit }
format.json { render json: @home.errors, status: :unprocessable_entity }
end
end
end
# DELETE /homes/1
# DELETE /homes/1.json
def destroy
@home.destroy
respond_to do |format|
format.html { redirect_to homes_url, notice: 'Home was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_home
@home = Home.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def home_params
params.require(:home).permit(:email)
end
end
homes/_form.html.erb
==============
<%= form_for(home) do |f| %>
<% if home.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(home.errors.count, "error") %> prohibited this home from being saved:</h2>
<ul>
<% home.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :email %>
<%= f.text_field :email, id: "email" %>
<span id ="error-span"></span>
</div>
<button type="button" id="btns" required="true">verify</button>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<script>
$('#btns').on('click', function(){
var email = $('#email').val();
$.ajax({
type:'POST',
url: '/homes/verify',
data: $.param({ homes: {email: email }})
});
})
</script>
verify.js.erb
==============
$('#error-span').html('<%= @message %>');
routes.rb
============
Rails.application.routes.draw do
resources :homes
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
post '/homes/verify' => 'homes#verify'
end
No comments:
Post a Comment