I am re-writing an App called Emissions Gateway from Rails v3.2 to Rails v6.1.4.1
I have about 99% of the app re-written and working but one part is stumping me. When you go to delete a user it requires that you move any projects they have to a different user. This normally displays a drop down menu. The modal is working the buttons are working but the drop down isn't displaying anything and I haven't been able to figure out WHY?
Here is the specific section that has the select_tag:
= simple_form_for @user, :html => { :method => :delete } do |f|
= select_tag :user_id, options_from_collection_for_select(users_when_being_deleted(@user), "id", "name")
Here is the method called users_when_being_deleted in a file called users_helper.rb
def users_when_being_deleted(user = nil)
if user
User.where(vendor_id: "current_user.vendor_id").
and(User.where.not(id: @user.id)).order(:name)
else
User.where(vendor_id: "current_user.vendor_id").order(:name)
end
end
Here is a picture of what the modal box looks like when you click on the down arrow:
Here is the destroy method in the users_controller.rb
def destroy
# check for vendor manager
@user = User.find(params[:id])
@vendor = @user.vendor
if (@user.id != @vendor.user_manager_id) && params[:user_id].present?
@user.move_data_to_user(params[:user_id])
@user.destroy
flash[:success] = "User removed."
redirect_to users_url
else
redirect_to users_url, notice: "Cannot delete this user."
end
end
Here is the move_data_to_user method in the model user.rb
def move_data_to_user(new_user_id)
@new_user = User.find_by_id(new_user_id)
self.articles.each { |a| a.user_id = new_user_id ; a.save }
self.audits.update_all(user_id: nil)
self.projects.each do |project|
Audit.create(class_kind: "project", item_id: project.id, action: "unassigned", new_value: self.name, user_id: User.current_user.id, project_id: project.id)
Audit.create(class_kind: "project", item_id: project.id, action: "assigned", new_value: @new_user.name, user_id: User.current_user.id, project_id: project.id) unless @new_user.nil?
end
self.projects.update_all(user_id: new_user_id, vendor_id: self.vendor_id)
self.verified_projects.update_all(user_verified_id: new_user_id)
self.pending_projects.update_all(user_submitted_id: new_user_id)
@new_user.touch
end
Here is the entire view show.html.haml:
- provide(:title, @user.name + " - Profile")
.container-fluid
#pad-wrapper.user-profile
// header
.row-fluid.header
.span8
%h3.name{style: "float:none;"}= @user.name
%span.area #{@user.projects.count} projects
- if can?(:destroy, @user) && (@user.id != @vendor.user_manager_id)
= link_to "#", type: "button", id: "user-delete-shortcut", class: "btn btn-danger icon pull-right delete-user", data: { backdrop: "static", toggle: "modal", target: "#user-delete-modal" } do
%i.icon-trash
#user-delete-modal.modal.hide.fade{"aria-hidden" => "true", "aria-labelledby" => "user-delete-modal", :role => "dialog", :tabindex => "-1"}
.modal-header.clearfix
%button.close{"aria-hidden" => "true", "data-dismiss" => "modal", :type => "button"} ×
%h3#UserDeleteModal Delete User
.modal-body
.row-fluid
.span12
.alert.alert-warning
%strong
In order to remove this user, you must first reassign their projects to another employee.
.row-fluid
.span6.offset3.center
= simple_form_for @user, :html => { :method => :delete } do |f|
= select_tag :user_id, options_from_collection_for_select(users_when_being_deleted(@user), "id", "name")
%br/
%br/
= f.submit "Move Projects & Destroy User", class: 'btn btn-danger center', data: { confirm: 'Are you sure? This cannot be undone!' }
.modal-footer
%ul.step_nav
%li.next
/ = f.submit class: "btn btn-success", name: "Confirm Delete User", value: "Save"
%li.previous
%button.btn.btn-danger{"aria-hidden" => "true", "data-dismiss" => "modal"} Close
- if can?(:edit, @user)
%a.btn.icon.btn-info.pull-right.large.edit{href: edit_user_path(@user)}
%i.icon-edit
Edit #{@user.name}
.row-fluid.profile
.span9.bio
.row-fluid
.span12
.profile-box
- if @recent_projects.any?
.span6
%h6 Recent projects
%br/
.span6
.span{style: 'padding-right: 10px;'}
= will_paginate(@recent_projects, param_name: 'project_page')
// projects table
.table-projects{style: "font-size: 15px;"}
.row-fluid
%table.table.table-hover
%thead
%tr
%th.span3
Name
%th.span3.hidden-phone
%span.line
Assessment
%th.span3.hidden-phone
%span.line
Datalogs
%th.span3.hidden-phone
%span.line
Pictures
%th.span3
%span.line
Status
%th.span3
%span.line
Created
%tbody
- cache ["v1", @recent_projects] do
= render @recent_projects
- else
%h5.center This user doesn't have any projects yet.
// Disabled notes, can reenable if use arrises
/ .span12.section.comment
/ %h6 Add a quick note
/ %p Add a note about this user to keep a history of your interactions.
/ %textarea
/ .span12.submit-box.pull-right
/ %input.btn-glow.primary{:type => "submit", :value => "Add Note"}/
/ %span OR
/ %input.reset{:type => "reset", :value => "Cancel"}/
.row-fluid
.profile-box
.span12
%h6
%i.icon-star
Wishes
%br/
.row-fluid
-if @wishes.any?
%table.table.table-hover
%thead
%tr
%th.span12
Wish
%th.span3
%span.line
Date
%th.span3
%span.line
Browser
%th.span3{colspan: 3}
%span.line
Status
%tbody
- @wishes.each do |wish|
= render "user_wishes", wish: wish
-else
.center
%h4
%i.icon-info-sign
No wishes have been submitted yet.
%br
%br
.row-fluid
.profile-box
- if @audits.any?
.span6
%h6 Recent Audits
%br/
.span6
.span{style: 'padding-right: 10px;'}
= will_paginate(@audits, param_name: 'audit_page')
// audits table
.table-audits{style: "font-size: 15px;"}
.row-fluid
%table.table.table-hover
%thead
%tr
%th.span3
Timestamp
%th.span3.hidden-phone
%span.line
User
%th.span3.hidden-phone
%span.line
Action
%th.span3.hidden-phone
%span.line
Section
%th.span3.hidden-phone
%span.line
Details
%tbody
- cache ["v1", @audits] do
= render @audits
- else
%h5.center This user doesn't have any projects yet.
// Disabled notes, can reenable if use arrises
/ .span12.section.comment
/ %h6 Add a quick note
/ %p Add a note about this user to keep a history of your interactions.
/ %textarea
/ .span12.submit-box.pull-right
/ %input.btn-glow.primary{:type => "submit", :value => "Add Note"}/
/ %span OR
/ %input.reset{:type => "reset", :value => "Cancel"}/
// side column
.span3.address.pull-right
.row-fluid
%h6.center User Details
#user-more-information.row-fluid.center
.span6
%dl
%dt Vendor
%dd= @user.vendor.name
%dt Role
%dd= @user.role.titleize
%dt Member Since
%dd= @user.created_at.strftime("%B #{@user.created_at.day.ordinalize}, %Y")
.span6
%dl
%dt Last Logged In
%dd= @user.last_sign_in_at ? @user.last_sign_in_at.strftime("%m/%d/%Y") : "Never"
%dt Current IP
%dd= @user.current_sign_in_ip
%dt Last IP
%dd= @user.last_sign_in_ip
.row-fluid.center.section
%h6 Project Stats
#user-project-stats
.span6
%dl
%dt Total
%dd= @user.projects.count
.span6
%dl
%dt Incomplete
%dd= @user.projects.incomplete.count
%dt Prepared
%dd= @user.projects.prepared.count
%dt Pending
%dd= @user.projects.pending.count
%dt Verified
%dd= @user.projects.verified.count
.row-fluid
.span2
= link_to 'Back', vendor_users_path(@user.vendor_id), class: 'btn'
If there is anything anyone else wants to see let me know. I don't believe there is any separate JavaScript in play beyond the bootstrap modal. Everything else is pretty much working. I am getting NO errors in the console about anything, no errors in the Rails console either, its as if once it gets to that modal nothing is happening. I'm stumped.
Any help would be greatly appreciated! Thank You for your time, Scott
I think the issue is with the query.
Change
User.where(vendor_id: "current_user.vendor_id")
To
User.where(vendor_id: current_user.vendor_id)