Home > front end >  how to render current_user after login to a Bootstrap input form in rails
how to render current_user after login to a Bootstrap input form in rails

Time:04-04

This might have already been asked, working on a ruby on rails bcrypt // authentication project that I am testing different methods for checking user login both in the view, and within an bootstrap form. but I am however having trouble trying to render my user controller current_user :id (after login) to (render) prefill an input form value

what I am trying to do is have my controller or modal change the value of "@helloguy123" in the plaintext field with my current_user(params[:id]) to show my user is login to a dashboard page and prefills this value space with user ID, so if the user wants to "change password" they are login to there user space, and my form check shows true that they are login, and ID of the user is showing on the page.

routes

Rails.application.routes.draw do
  get '/users/', to: 'user#index'
  get '/users/:id', to: 'user#show'
  get '/register/',  to: 'register#index'
  get '/checkuser/', to: 'user#validate'
  get '/login', to: 'sessions#new'
  get '/logout', to: 'sessions#destroy'
  get '/', to: 'home#index'
  get '/dashboard', to: 'dashboard#index'

  post '/register/', to: 'register#createuser'  
  post '/login', to: 'sessions#create'

end

user controller:

class UserController < ApplicationController
   def index
    # rendering json on page: user data
      render json: User.all
   end

   def create
      user = User.new(
         first_name: params[:fname], 
         last_name: params[:lname],
         username: params[:username],
         password: params[:password],
         password_confirmation: params[:password_confirmation]
      end

   def show
      #  render plain: params[:id]
      #render json: userdata[:users].select {|user| user.get_id() == params[:id].to_i}
      if User.exists?(params[:id])
         render json: User.find(params[:id].to_i)
      else 
         render plain: "that user doesnt exist: #{params[:id]}"
      end
   end

   def validate
      puts params
      username = params[:username]
      exists = User.exists?(username: username)
      # puts exists
      render json: {"exists": exists, "username": username}
   end
end

input form I am using from bootstrap

<% content_for :content do %>

<form >
 <div >
  <label for="username" ></label>
  <input type="text" readonly  id="username" value="@Helloguy123">
 </div>
 
 <div >
 <label for="password" >Password</label>
  <input type="password"  id="password" placeholder="Password">
 </div>

 <div >
  <button type="submit" >Confirm identity</button>
 </div>
</form>

<% end %>

<%= render template: "layouts/application" %>

CodePudding user response:

As I understand you already have current_user helper

So you can use it

<input
  type="text"
  readonly
  
  id="username"
  value="<%= current_user&.username || "@Helloguy123" %>"
>
  • Related