Rails 4 Update Data Through View Page -



Rails 4 Update Data Through View Page -

i have view page named "users". in view page, i'm displaying table attributes pulled users table such first name, lastly name, lastly login, admin, etc... in admin field, displays users table true or false.

what accomplish create drop down, displays user's current value admin (true or false), , able alter attribute view page. currently, i'm having utilize console on server manually alter user's value true or false grant or revoke admin privileges. needless say, inconvenient. i'm still new rails , have been using scaffolds point. i'm not sure how accomplish this, , advise appreciated!

users schema:

create_table "users", force: true |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.integer "sign_in_count", default: 0, null: false t.datetime "current_sign_in_at" t.datetime "last_sign_in_at" t.string "current_sign_in_ip" t.string "last_sign_in_ip" t.datetime "created_at" t.datetime "updated_at" t.boolean "admin", default: false t.string "first_name" t.string "last_name" end

admin controller:

class admincontroller < applicationcontroller before_action :authenticate_user! def users @users = user.all.order('last_name asc') end def reports end end

users view page:

<table class="table table-hover table-condensed"> <thead> <tr> <th>last name</th> <th>first name</th> <th>email</th> <th>creation date</th> <th>last sign in</th> <th>ip address</th> <th>admin</th> </tr> </thead> <tbody> <% @users.each |user| %> <tr> <td><%= user.last_name %></td> <td><%= user.first_name %></td> <td><%= user.email %></td> <td><%= user.created_at.strftime("%-m/%-d/%y") %></td> <td><%= user.current_sign_in_at.strftime("%-m/%-d/%y") %></td> <td><%= user.current_sign_in_ip %></td> <td><%= user.admin %></td> </tr> <% end %> </tbody> </table>

you accomplish in couple of different ways. example, allow link in drop-down trigger method toggle_admin removing admin if user admin or reverse if that's case.

first create route, in routes.rb:

resource :users :toggle_admin end

in view:

<%=link_to (@current_user.admin ? 'remove admin' : 'make admin'), toggle_admin_users_path %>

and in controller, have this:

def toggle_admin @current_user.update_attribute :admin, (@current_user.admin ? false : true) end

hmm... yeah, don't know. work. can see, assume here have @current_user variable picking relevant user, otherwise send in user.id these methods, either url params or straight controller method.

ruby-on-rails ruby-on-rails-4 ruby-on-rails-4.1

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -