Edit other user as admin in devise, ruby on rails

前端 未结 1 1102
挽巷
挽巷 2021-01-20 14:11

I\'m using devise in my Ruby on Rails project and users have an admin-attribute:

schema.rb:

     create_table \"users\", force: :cascade do |t|
              


        
相关标签:
1条回答
  • 2021-01-20 14:57

    I figured it out:

    1. add a prefix in routes on devise_for :users,: :path_prefix => 'my'
    2. add this below devise_for: resources :users
    3. copy edit.html.erb from the registration from devise to users.
    4. Change <%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>

    to: <%= form_for(@user) do |f| %>

    1. Add this to the usercontroller:

         def edit
           @user = User.find(params[:id])
         end
      
      
         def update
           @user = User.find(params[:id])
           if @user.update(user_params)
             redirect_to adminpanel_path
           else
             render 'edit'
           end
         end
      

    Done. (strangely, I can't put number 5 in code)

    0 讨论(0)
提交回复
热议问题