Bootstrap-form-validation:






Link timer Clock Set Time:
https://codepen.io/SitePoint/pen/MwNPVq


User Authentication: Devise gem


Link: https://github.com/plataformatec/devise


1. Add devise gem in to Gemfile
  gem 'devise'

2. Run “bundle install”

3. Next, you need to run the generator:

  $ rails generate devise:install

4. Add file: Here is a possible configuration for config/environments/development.rb:
config.action_mailer.default_url_options
= { host: 'localhost', port: 3000 }
5. Generate model

  rails generate devise MODEL

MODEL cam be User, Admin, Customer e.t.c


6. Run “rake db:migrate

7. Restart server

8.Controller filters and helpers: app/controller/ module controller like articles_controller.rb :

before_action :authenticate_user!

9. Configuring views:

$ rails generate devise:views

10. Add Signount links: app/view/layout/application.html.erb

<div id="user_nav">
<% if user_signed_in? %>
Signed in as <%= current_user.email %>. Not you?
<%= link_to "Sign out", destroy_user_session_path, :method => :delete %>
<% else %>
<%= link_to "Sign up", new_user_registration_path %> or <%= link_to "sign in", new_user_session_path %>
<% end %>
</div>



11.SMTP Credentials : its use for get password by mailer app/config/ envernments/development.rb
development.rb

config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'localhost',
user_name: 'testing.bittern@gmail.com',
password: 'bittern1234',
authentication: 'plain',
enable_starttls_auto: true }

12. Add new columns:
1. Add new migration:
1. $ rails generate migration add_last_name_to_users last_name:string
ex
$ rails g migration add category_id_to_blogs category_id:string
Ex= $ rails g migration add_column_name_to_table name column_name:type(string,integer,text)

2. $ rake db:migrate



13.Permit new parameteres in devise: app → controller → applicationcontroller.rb
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :configure_permitted_parameters, if: :devise_controller?

protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name, :last_name])
  end
end












14. Only logged in user can create the new blog, edit blog and delete blog and logged in user can see his blog after sign in.

app/controller/blogs_controller.rb

create action method in controller

def create
@blog=Blog.new(blog_params)
@blog.user_id = current_user.id
@blog.save
redirect_to @blog
end

def my_blog
@blogs= Blog.where(:user_id => current_user.id)
end

Give the link: app/view/layout/application.html.erb

<%=link_to “my Blogs”,”/my_blogs” %>

change in app/view/index.html.erb
<% if current_user_id==blog.user_id %>
<% end%>


Show Category_id :


index .html.erb

       <td>
         <%if blog.category_id.present? %>
         <%= Category.find(blog.category_id).name %>
        <% end %>
        </td>

edit.html.erb

<p>
<%= f.select(:category_id, Category.all.collect {|u| [u.name, u.id]}, :prompt => 'Select Category') %>
</p>




show.html.erb

<p>
   <%if @blog.category_id.present? %>

    <strong>Category:</strong>
    <%=Category.find(@blog.category_id).name%>
    <%end%>
 </p>






Active Admin Documentation

Active Admin is a framework for creating administration style interfaces. It abstracts common business application patterns to make it simple for developers to implement beautiful and elegant interfaces with very little effort.

Getting Started

Active Admin is released as a Ruby Gem. The gem is to be installed within a Ruby on Rails 4 application. To install, simply add the following to your Gemfile:
# Gemfile
gem
'activeadmin', github: 'activeadmin'
After updating your bundle, run the installer
rails
generate active_admin:install
The installer creates an initializer used for configuring defaults used by Active Admin as well as a new folder at app/admin to put all your admin configurations.
Migrate your db and start the server:
$> rake db:migrate
$>
rails server
Visit http://localhost:3000/admin and log in using:
  • User: admin@example.com
  • Password: password


Voila! You’re on your brand new Active Admin dashboard.
To register your first model, run:
$> rails generate
active_admin:resource

       [MyModelName] like- Blog
This creates a file at app/admin/my_model_names.rb for configuring the resource. Refresh your web browser to see the interface.




Tasks to do in the App:

1. Create a new application using database postgres.
2. Integrate devise for signin, signup, signout and forgot password.
3. Integrate active admin(https://github.com/).
4. Allow admin to create the categories.
5. Allow user to create the blogs with title, image, dropdown category(select_tag) and description.
6. Allow user to search the blogs with keyword(Reference: https://rubyplus.com/articles/).
7. Allow user to filter the blogs within a date range.
8. Allow user to comment on the blogs.
9. Integrate the template in bootstrap (after completing the above points, refenrece: https://github.com/twbs/).

https://github.com/twbs/bootstrap-sass
10. Push the application to github.
11. Deploy the application to heroku.






Search:
In view: layout
<%=form_tag "/blogs" ,:method=>"GET" do %>
<%= text_field_tag :search, params[:search] , :placeholder => "Type here to search"%>
<%= submit_tag "Search"%>
<%end%>

In controller:
if params[:search].present?
@blogs= Blog.where('title LIKE ? OR description LIKE ?', "%#{params[:search]}%", "%#{params[:search]}%")
elsif params[:start_date].present? && params[:end_date].present?
@blogs = Blog.where('created_at > ? AND created_at < ?', params[:start_date].to_date, params[:end_date].to_date)
else
@blogs=Blog.all
end
In index:
<% if params[:search].present? %>
<h3> <%= @blogs.count%> Results found.</h3>
<%else%>
<h3> No Result found.</h3>
<%end%>




Rails Active Admin css Rconflicting with Twitter Bootstrap css


Links:http://bootstrapdocs.com/v3.0.3/docs/css/#forms
For me changing application.css to following solves the problem:
*=
stub "active_admin"


Bootstrap for Sass using gem



In your Gemfile you need to add the bootstrap-sass gem, and ensure that the sass-rails gem is present - it is added to new Rails applications by default.
gem
'bootstrap-sass',
'~>
3.3.6'
gem
'sass-rails',
'>=
3.2'
 bundle
install

Import Bootstrap styles in app/assets/stylesheets/application.scss:
//
"bootstrap-sprockets" must be imported before "bootstrap"
and "bootstrap/variables"
@import
"bootstrap-sprockets";
@import
"bootstrap";



Allow user to search the blogs with keyword
Create
Search Bar:
using
create search bar for any module
application/layout
<%=form_tag "/blogs" ,:method=>"GET" do %>


<%= text_field_tag :search, params[:search] , :placeholder => "Type here to search"%>
<%= text_field_tag :start_date, params[:start_date] , :placeholder => "Start date", :id=>"start-date"%>
<%= text_field_tag :end_date, params[:end_date] , :placeholder => "end_date" , :id=>"end-date"%>
<%= submit_tag "Search", :class=>"btn btn-success"%>
<%end%>








Allow user to filter the blogs within a date range


layout script for datepicker rails form:




<script>
$( function() {
$("#start-date" ).datepicker({ dateFormat: 'dd-mm-yy' });
$("#end-date" ).datepicker({ dateFormat: 'dd-mm-yy' });
} );
</script>


Using Reply:

In _comment.html.erb

</p>
<h2> reply a comment</h2>
<% @replies= Reply.where(:comment_id=> comment.id)
%>

<%@replies.each do |reply|%>
<p><strong>Reply:</strong><%=reply.body%></p>
<%end%>
<%= render 'replies/form', :comment=>comment %>


In replies:
in _form.html.erb

<%= form_for :reply, url: "/articles/#{@article.id}/comments/#{comment.id}/replies" do |f| %>
<%= f.hidden_field :comment_id, :value=> comment.id %>
<%= f.text_field :body %>
<%= f.submit %>
<% end %>

In _reply.html.erb

<p>
<%= comment.body %>
<%= render comment.replies if comment.replies.any? %>
</p>



In controller:

replies_controller.rb

class RepliesController < ApplicationController
before_action do
@comment = Comment.find(params[:comment_id])
end

def index
@replies = @comment.replies
@reply = @comment.replies.new
end

def new
@reply = @comment.replies.new
end

def create
@reply = Reply.new(:body=>params[:reply][:body],:comment_id=>params[:reply][:comment_id])
@reply.save
flash[:notice]= "replies created"
redirect_to "/articles/#{params[:article_id]}"
#redirect somewhere
end

end


Using image:





AJAX: make request without page loading:

AJAX: make request without page loading:
Add remote= true in form or Anchor tag:
<%= form_for([@blog, @blog.comments.build], :remote => true) do |f| %> 
    <%= f.label :body %><br>
    <%= f.text_area :body %>   
    <%= f.submit %>
<% end %>

<a herf=”url” data-remote=true> Hello </a>
<%= link_to “hello”, root_url, :remote => true %>

In Controller: request will be come in AS JS format
def create
    @blog = Blog.find(params[:blog_id])
    @comment = @blog.comments.create(comment_
    render 'blogs/create_comments'
  end





In Views:

app/blogs/create_comments.js.
$('.comments-container').html(
$("#new_comment")[0].reset();
$("#new_comment").hide();


In App/views/comments/_comments.

<%@blog.comments.each do |comment|%> 
<p>
  <strong>Commenter:</strong>
  <%= comment.commenter %>
</p>
 
<p>
  <strong>Comment:</strong>
  <%= comment.body %>
</p>
 
<p>
  <%= link_to 'Destroy Comment', [comment.blog, comment],
               method: :delete,
               data: { confirm: 'Are you sure?' } %>
</p>
<p>
  <%= link_to 'Edit Comment', edit_blog_comment_path(
</p>

<h2> reply a comment</h2>
<% @replies=Reply.where(:comment_comment.id)
%>

<%@replies.each do |reply|%>
  <p><strong>Reply:</strong>><%=
<%end%>


<%= render 'replies/form', :comment=>comment %>
<%end%>
AJAX:
make
request without page loading:
Add
remote=
true in form or Anchor tag:
<%=
form_for([@blog,
@blog.comments.build], :remote => true) do |f|
%>







  <%= f.label :body %><br>



  <%= f.text_area :body %>

 



  <%= f.submit %>
<%
end
%>


<a
herf=”url”
data-remote=true> Hello </a>
<%=
link_to
“hello”, root_url, :remote => true %>


In
Controller:
request will be come in
AS
JS format
def
create



  @blog = Blog.find(params[:blog_id])



  @comment =
@blog.comments.create(comment_params)



  render 'blogs/create_comments'



end
Note: user can use Bootstrap UI from here http://bootsnipp.com/tags




<!DOCTYPE html>
<html>
  <head>
    <title>Voter1</title>
    <%= csrf_meta_tags %>

    <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
  </head>

<body>
<nav class="navbar navbar-default navbar-static-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">Project name</a>
        </div>
        <div id="navbar" class="navbar-collapse collapse" aria-expanded="false" style="height: 1px;">
          <ul class="nav navbar-nav">
            <li class="active"><a href="#">Home</a></li>
            <li><a href="#about">About</a></li>
            <li><a href="#contact">Contact</a></li>
            <li class="dropdown">
              <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
              <ul class="dropdown-menu">
                <li><a href="#">Action</a></li>
                <li><a href="#">Another action</a></li>
                <li><a href="#">Something else here</a></li>
                <li role="separator" class="divider"></li>
                <li class="dropdown-header">Nav header</li>
                <li><a href="#">Separated link</a></li>
                <li><a href="#">One more separated link</a></li>
              </ul>
            </li>
          </ul>
          <ul class="nav navbar-nav navbar-right">
            <% unless user_signed_in? %>
            <li><a href="/users/sign_in">Login</a></li>
            <li><a href="/users/sign_up">Register</a></li>
            <% else %>
               <li><%= link_to "Sign out", destroy_user_session_path, :method => :delete %></li>
            <% end%>
            <li class="active"><a href="./">Static top <span class="sr-only">(current)</span></a></li>
            <li><a href="../navbar-fixed-top/">Fixed top</a></li>
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </nav>
<div id="user_name">
  <% if user_signed_in? %>
    Signed in as <%= current_user.email %>. Not you?
    <%= link_to "Sign out", destroy_user_session_path, :method => :delete %>
    <%= link_to "Sign out", destroy_user_session_path, :method => :delete %>

     <% else %>
    <%= link_to "Sign up", new_user_registration_path %> or <%= link_to "sign in", new_user_session_path %>
  <% end%>
  </div>
<br>


   <%= link_to 'admin','admin' %>|| <%= link_to 'result','/result' %> || <%= link_to 'new','/votes/new' %> || <%= link_to 'votes','/votes' %> ||

    <%= yield %>
    <footer class="footer">
      <div class="container">
        <p class="text-muted">Place sticky footer content here.</p>
      </div>
    </footer>
  </body>
</html>



in application.rb remove inherite

config.app_generators.scaffold_controller= :scaffold_controller


Command run scaffold

rails
g scaffold category name:string desc:text


rails
generate migration AddColumnsToProducts category_id —force


rake
db:migrate
link: https://blog.skcript.com/implementing-categories-in-ruby-on-rails-14c2b5e77b34
#app/models/product.rb
belongs_to
:category
And in your Category model,
#app/models/category.rb
has_many :products


preview and upload image in jquery

<td>Company Logo:<%= file_field_tag :avatar,name: "avatar",:onchange => "showimagepreview(this)",:class=>"img_file" %></td>
    <img id="imgprvw" style="width: 167px; height: 94px; margin-top: 8px;" />

<script>
  function showimagepreview(input) {
    if (input.files && input.files[0]) {
    var filerdr = new FileReader();
    filerdr.onload = function(e) {

    $('#imgprvw').attr('src', e.target.result);
    }
    filerdr.readAsDataURL(input.files[0]);
    }
    $('.image_upload').submit();
  }
</script>

Comments

Popular posts from this blog

User Login And Logout Show Notice Box Massage

Add CORS to Nginx on AWS Elastic Beanstalk

dmp file