Translate To Preferred Language

Search ObiokusThoughts

Please Read Today's Featured Post

Alliteration Ere Zeitgeist

Actually ask archaic adult Broadband boy baked bad batch Cold case cant consistently catch Dawn developed dusk do-over Enact emerge...

Template of WebLogins Scaffold in Ruby on Rails (some bugs may occur)

---Controller---
class WebLoginsController < ApplicationController
  before_action :set_web_login, only: [:show, :edit, :update, :destroy]

  # GET /web_logins
  # GET /web_logins.json
  def index
    @web_logins = WebLogin.order(:name)
  end

  # GET /web_logins/1
  # GET /web_logins/1.json
  def show
  end

  # GET /web_logins/new
  def new
    @web_login = WebLogin.new
  end

  # GET /web_logins/1/edit
  def edit
  end

  # POST /web_logins
  # POST /web_logins.json
  def create
    @web_login = WebLogin.new(web_login_params)

    respond_to do |format|
      if @web_login.save
        format.html { redirect_to @web_login, notice: 'Web login was successfully created.' }
        format.json { render :show, status: :created, location: @web_login }
      else
        format.html { render :new }
        format.json { render json: @web_login.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /web_logins/1
  # PATCH/PUT /web_logins/1.json
  def update
    respond_to do |format|
      if @web_login.update(web_login_params)
        format.html { redirect_to @web_login, notice: 'Web login was successfully updated.' }
        format.json { render :show, status: :ok, location: @web_login }
      else
        format.html { render :edit }
        format.json { render json: @web_login.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /web_logins/1
  # DELETE /web_logins/1.json
  def destroy
    @student = Student.where(["web_logins_id = ?", @web_login.id])
    @web_login.destroy
    @student.each do |stu_web|
      stu_web.destroy
    end
    respond_to do |format|
      format.html { redirect_to web_logins_url, notice: 'Web login was successfully deleted.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_web_login
      @web_login = WebLogin.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def web_login_params
      params.require(:web_login).permit(:name, :password)
    end
 
    def encrypt_pass
      salt = BCrypt::Engine.generate_salt
      encypted = BCrypt::Engine.hash_secret(:password, salt)
    end
end
---Model---
class WebLogin < ActiveRecord::Base
    #belongs_to :students 
    has_secure_password
    validates :password, presence: true, length: {in: 6..14}, confirmation: true
    validates :name, presence: true, uniqueness: true, length: {in: 2..20}
    #scope :sorted, lambda {order("web_logins.position ASC")}
end
---form---
<%#Default Form for the Web Login views %>
<% provide(:title, "Login Page") %>
<%= form_for(@web_login) do |f| %>
  <% if @web_login.errors.any? %>
   
     

<%= pluralize(@web_login.errors.count, "error") %> prohibited this web_login from being saved:


     
          <% @web_login.errors.full_messages.each do |message| %>
           




  • <%= message %>
  •       <% end %>
         
       
      <% end %>

     
        <%= f.label :name %>
        <%= f.text_field :name %>
     
     
        <%= f.label :password %>
        <%= f.password_field :password %>
     
     
        <%= f.label :password_confirmation %>
        <%= f.password_field :password_confirmation %>
     
     
        <%= f.submit %>
     
    <% end %>
    ---Edit---
    <%# Edit name or encypted password of selected entry%>

    Editing Web Login

    <%= render 'form' %>

    <%= link_to 'Show', @web_login %> |
    <%= link_to 'Back', web_logins_path %>
    ---Index---
    <%# Show name and encrypted password for each saved login%>
    <% provide(:title, "Login Page") %>
    <%= notice %>

    Users Create Login Here

    <%# call javascript function as greeting alert%>
    <%= javascript_tag("welcome();") %>
     
       
         
    Name
         
    Encrypted Password
         
    Links
       
     
     
        <% @web_logins.each do |web_login| %>
         
           
    <%= web_login.name %>
           
    <%= web_login.password_digest %>
           
    <%= link_to 'Show', web_login %>
           
    <%= link_to 'Edit', edit_web_login_path(web_login) %>
           
    <%= link_to 'Delete', web_login, method: :delete, data: { confirm: 'Are you sure?' } %>
           
    <%= link_to 'Add Student', new_web_login_student_path(web_login) %>
         
        <% end %>
     



    <%= link_to 'New Web login', new_web_login_path %>
    ---New---
    <%# Create and save new web login%>
    <%= provide(:title, 'Enter Your Information') %>

    New Web Login

    <%= render 'form' %>

    <%= link_to 'Back', web_logins_path %>
    ---Show---
    <%# Show name and encypted password of selected entry%>
    <% provide(:title, "View Login") %>
    <%= notice %>
      Name:
      <%= @web_login.name %>

      Password:
      <%= @web_login.password_digest %>

    <%= link_to 'Edit', edit_web_login_path(@web_login) %> |
    <%= link_to 'New Student', new_web_login_student_path(@web_login.id) %> |
    <%= link_to 'Back', web_logins_path %>
    ---Migration---
    class CreateWebLogins < ActiveRecord::Migration
      #belongs_to :students
      def change
        change_table :web_logins do |t|
          t.string :name
          t.string :password

          t.timestamps null: false
        end
      end
    end
    ---Parts of Routes---
    Rails.application.routes.draw do
      resources :courses
      resources :majors
      resources :students
      resources :web_logins
      #get 'web_logins/returning'

      resources :web_logins do 
        collection do
          get :search
        end
      end
      
      resources :web_logins do
        resources :students
      end
      
      scope 'web_logins' do
        resources :students, as: 'web_logins_students'
      end
      
    root 'web_logins#index'
       match ':controller(/:action(/:id))', :via => [:get, :post] 

    No comments:

    Post a Comment

    Thank you for reading.
    Please share your thoughts.
    Be blessed and enjoy life!