-2

I am using devise for authentication on a client app. After extensive reading of the wiki, I managed to configure my app to suit my needs properly. However I have a CSS loading problem.
I chose the option of allowing an admin approve an account over :confirmable as I am not familiar with combining both :confirmable and approving the account.

I followed the instructions in "How To: Require admin to activate account before sign_in" to do just that and "How to do browser-based new user approval by Admin in Rails 3.2 using Devise" to help with configuring my routes.rb file when updating the attribute.

The issue now is, when I click the link to update the attribute, it does so flawlessly but the path for finding CSS after redirecting back changes.

To clarify what I mean, let's say my CSS file is in public/user/mycss.css, when I sign in with devise, it loads properly but upon clicking the link_to tag it updates the record, but looks for mycss.css via users/:id/approve/mycss.css which doesn't exist with reference to the second link.

I have an admin model that allows the client to create posts for a blog. The authentication system works flawlessly. The devise wiki link in the first link suggested that I list all the users and create a method that is simple and efficient to update the approved attribute. In my controller I have this code:

def approve_admin
    admin = Admin.find(params[:id])
    unless admin.approved?
      admin.update_attribute :approved, true
      if admin.save
        flash[:notice] = "#{admin.first_name} approved"
      else
        flash[:alert] = "#{admin.first_name} approval failure"
      end
    end
    redirect_to :back
  end

In my view I use a link_to tag that updates the attribute using the above method:

<% if !(admin.approved?) %>
  <p class="hvr-shutter-out-horizontal hvr-grow-shadow"><%= link_to "Approve", approve_admin_path(admin.id)  %></p>
<% end %>

In my routes file I have this:

get 'admin/:id/approve', to: 'posts#approve_admin', as: 'approve_admin'

This works perfectly but then the CSS and JavaScript files do not load. I inspected the browser for errors and this was the output:

http://localhost:3000/admin/3/js/modernizr.custom.js

for example, instead of loading the files from

http://localhost:3000/admin/js/modernizr.custom.js

Why is it looking for the CSS file via that path?

Community
  • 1
  • 1
mrtorks
  • 66
  • 2
  • 11
  • The file path looks to me like you aren't using the asset pipeline. Is that correct? Where do you store the css files, in the public folder? How to you link the css files in your layout files? – spickermann Aug 15 '16 at 09:20
  • @spickermann I store the css files in the public folder. In my layout files I link the files using regular html stylesheet links. – mrtorks Aug 15 '16 at 09:31
  • Seems like you are using relative urls in the stylesheet tags. You might want to try absolute links instead. – spickermann Aug 15 '16 at 11:35
  • Please read "[ask]" including the linked pages, and "[mcve]". You're asking us to imagine your Rails code, which is not easily done, then to figure out a solution to a problem we can't see. That's not likely to work well. We need to see the minimum code that duplicates the problem plus any supporting input data and your expected results. Also, instead of saying "this links" and "this link", summarize what is important from those links and then refer to the pages. That way, _when_ the links rot and break your question will continue to make sense to those searching for a similar solution. – the Tin Man Aug 15 '16 at 20:41
  • @theTinMan thanks for pointing me in the right direction in terms of clarifying the question. I have updated it – mrtorks Aug 15 '16 at 21:17

1 Answers1

0

Based on further research I came up with this answer, which could be a possible solution for anyone who is experiencing trouble with approving a user in devise via an admin. In my case I call them "admin".

After following all the steps in "How To: Require admin to activate account before sign_in" I created a method in the corresponding controller to update the attribute:

def approve_admin
    admin = Admin.find(params[:id])
    unless admin.approved?
      admin.update_attribute :approved, true
      if admin.save
        flash[:notice] = "#{admin.first_name} approved"
      else
        flash[:alert] = "#{admin.first_name} approval failure"
      end
    end
    redirect_to :back
  end

In my view I created a link_to tag that updates the attribute:

<% if !(admin.approved?) %>
  <p class="hvr-shutter-out-horizontal hvr-grow-shadow"><%= link_to "Approve", approve_admin_path(admin.id),method: :put  %></p>
<% end %>

In my routes file I did this:

put 'admin/:id/approve', to: 'posts#approve_admin', as: 'approve_admin'

I changed get to put and in my view I used method: :put. That told Rails to update the record without having to look for the CSS files somewhere else.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
mrtorks
  • 66
  • 2
  • 11