Last active
June 11, 2021 08:40
-
-
Save RailsCod3rFuture/073f1b5038e492d7b6f527c515be1905 to your computer and use it in GitHub Desktop.
Failing User Profile Controller, Routes and Partial Form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rails.application.routes.draw do | |
devise_for :admins | |
devise_for :users | |
namespace :api do | |
scope :v1 do | |
mount_devise_token_auth_for 'User', at: 'user_auth' | |
end | |
end | |
devise_scope :user do | |
authenticated do | |
root to: 'user_dashboard#index', as: 'authenticated_user_root' | |
end | |
unauthenticated do | |
root to: 'home#index', as: 'unauthenticated_user_root' | |
end | |
end | |
devise_scope :admin do | |
authenticated do | |
root to: 'admin_dashboard#admin', as: 'authenticated_admin_root' | |
end | |
unauthenticated do | |
root to: 'home#index', as: 'unauthenticated_admin_root' | |
end | |
end | |
controller :home do | |
get :index, to: 'home#index', as: 'home', :path => 'home' | |
get :login_portal, to: 'home#login_portal', as: 'login_portal', :path => 'login_portal' | |
get :signup_portal, to: 'home#signup_portal', as: 'signup_portal', :path => 'signup_portal' | |
end | |
resources :posts do | |
member do | |
post :share | |
put 'like', to: 'posts#like' | |
get 'like', to: 'posts#like' | |
end | |
end | |
resources :users, only: [:show, :index, :update], path: 'u' do | |
get 'users/:username' => 'users#show' | |
patch 'users/:username', to: 'users#update' | |
resources :follows, :only => [:create, :destroy] | |
end | |
resources :user_profiles, :only => [:edit, :update], path: 'profile' | |
root 'home#index' | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lass User < ApplicationRecord | |
# Include default devise modules. | |
devise :database_authenticatable, :registerable, | |
:recoverable, :rememberable, :trackable, :validatable, | |
:confirmable | |
include DeviseTokenAuth::Concerns::User | |
before_create :build_user_profile | |
extend FriendlyId | |
# Include default devise modules. Others available are: | |
# :confirmable, :lockable, :timeoutable and :omniauthable | |
devise :database_authenticatable, :registerable, | |
:recoverable, :rememberable, :trackable, :validatable | |
friendly_id :username, use: :slugged | |
has_one :user_profile | |
accepts_nested_attributes_for :user_profile, update_only: true | |
def user_address | |
[street, state, city_or_town, zip_code, country].compact.join(', ') | |
end | |
def user_full_name | |
first_name + " " + last_name | |
end | |
protected | |
def confirmation_required? | |
false | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= simple_form_for(@user, url: user_profile_path(@user.user_profile.id), :html => {:multipart => true}) do |f| %> | |
<%= f.error_notification %> | |
<%= f.simple_fields_for :user_profile do |user_profile| %> | |
<div class="form-group"> | |
<div class="col-md-6"> | |
<h4><%= fa_icon 'picture-o' %> My Avatar</h4> | |
<% if current_user.user_profile.avatar.url.present? %> | |
<%= image_tag(current_user.user_profile.avatar.url, class: 'd-flex align-self-start mr-3 img-thumbnail rounded-circle') %> | |
<%= user_profile.label :remove_avatar do %> | |
<%= user_profile.input :remove_avatar, as: :boolean, checked_value: true, unchecked_value: false %> | |
<% end %> | |
<% end %> | |
<div class="fileinput fileinput-new" data-provides="fileinput"> | |
<div class="fileinput-preview img-responsive thumbnail" data-trigger="fileinput" style="width: 200px; height: 146px; border: 1px #8d8d8d solid;"></div> | |
<div> | |
<label class="btn-flat btn-sm bttn-success choose-avatar-btn-bottom-padding"> | |
Add an Avatar | |
<span class="btn-file fileinput-new" style="display: none;"> | |
<%= user_profile.input :avatar, as: :file, class: 'form-control', label: false, id: 'avatar-upload' %> | |
</span> | |
</label> | |
<div class="form-group row" style="padding-top: 0.50em;"> | |
<div class="col-md-6"> | |
<span class="btn-file"><span class="fileinput-exists bttn-bordered bttn-md bttn-warning">Change</span><input type="file" name="..."></span> | |
</div> | |
<div class="col-md-6"> | |
<a href="#" class="btn btn-md btn-danger fileinput-exists" data-dismiss="fileinput">Remove</a> | |
</div> | |
</div> | |
</div> | |
</div> | |
<%= user_profile.hidden_field :avatar_cache %> | |
</div> | |
</div> | |
<div class="form-group"> | |
<div class="col-md-6"> | |
<h4>Birthday</h4> | |
<%= user_profile.input :birthday, start_year: Date.today.year - 110, end_year: Date.today.year, label: false, id: 'datepicker', class: 'form-control' %> | |
</div> | |
</div> | |
<div class="form-group"> | |
<div class="col-md-6"> | |
<h4>tags<small>(Separated by Commas)</small></h4> | |
<%= user_profile.input :tag_list, class: 'form-control', label: false, input_html: {data: {role: 'tagsinput'}}, placeholder: 'List your skills and abilities' %> | |
</div> | |
</div> | |
<% end %> | |
<div class="form-group"> | |
<div class="col-md-4"> | |
<%= f.button :submit, 'Update My Profile', class: 'btn btn-md btn-primary' %> | |
</div> | |
</div> | |
<% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserProfile < ApplicationRecord | |
acts_as_birthday :birthday | |
mount_uploader :avatar, UserAvatarUploader | |
belongs_to :user, :dependent => :destroy | |
validates_integrity_of :avatar | |
validates_processing_of :avatar | |
acts_as_taggable | |
acts_as_taggable_on :abilities | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserProfilesController < ApplicationController | |
before_action :authenticate_user! | |
before_action :set_user, only: [:edit, :update] | |
include UserProfilesHelper | |
def show | |
end | |
# GET /user_profiles/1/edit | |
def edit | |
end | |
# PATCH/PUT /user_profiles/1 | |
# PATCH/PUT /user_profiles/1.json | |
def update | |
@user_profile = @user.user_profile | |
respond_to do |format| | |
if @user_profile.update(user_profile_params) | |
@user_profile.save | |
format.html {redirect_to posts_path, notice: 'User profile was successfully updated.'} | |
format.json {render :show, status: :ok, location: @user_profile} | |
else | |
format.html {render :edit} | |
format.json {render json: @user_profile.errors, status: :unprocessable_entity} | |
end | |
end | |
end | |
private | |
def set_user | |
@user = User.friendly.find(params[:id]) | |
end | |
# Never trust parameters from the scary internet, only allow the white list through. | |
def user_profile_params | |
params.require(:user).permit(user_profile_attributes: [:status, :birthday, :user_id, :tag_list]) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UsersController < ApplicationController | |
#before_action :set_user, only: [:show] | |
def index | |
@users = User.all | |
respond_to do |format| | |
format.json{ render :json => User.where('username like ?', "#{q}%").pluck(:username).compact} | |
end | |
end | |
# GET /users/1 | |
# GET /users/1.json | |
def show | |
@user = User.friendly.find(params[:id]) | |
if request.path != user_path(@user) | |
redirect_to @user, status: 404 | |
end | |
respond_to do |format| | |
format.html | |
format.js {render json: @user} | |
end | |
end | |
def update | |
@user = User.friendly.find(params[:id]) | |
respond_to do |format| | |
if @user.user_profile.update_attributes(user_params) | |
format.html { redirect_to @user, notice: 'User was successfully updated.' } | |
format.json { head :no_content } | |
else | |
format.html { redirect_to edit_user_profile_path(@user) } | |
format.json { render json: @user.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
private | |
# Use callbacks to share common setup or constraints between actions. | |
def set_user | |
@user = User.friendly.find(params[:id].downcase) | |
end | |
# Never trust parameters from the scary internet, only allow the white list through. | |
def user_params | |
params.require(:user).permit(user_profile_attributes: [:status, :birthday, :avatar, :user_id, :tag_list, :id, :avatar_cache]) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment