Skip to content

Instantly share code, notes, and snippets.

@derigible
Created November 16, 2020 22:04
Show Gist options
  • Save derigible/2067c5b4df65c6f7453a3dfe9ecc5409 to your computer and use it in GitHub Desktop.
Save derigible/2067c5b4df65c6f7453a3dfe9ecc5409 to your computer and use it in GitHub Desktop.
admin page for user_report
Steps:
rails generate controller AdminPortal user_report user_lookup user_select_action
- Default generator creates a lot of files, these include the following:
create app/controllers/admin_portal_controller.rb - the controller
route get 'admin_portal/user_report' - updates the routes file to point to actions in controller
invoke erb
create app/views/admin_portal - admin_portal views folder
create app/views/admin_portal/user_report.html.erb - admin_portal#user_report action's view
create app/views/admin_portal/user_lookup.html.erb - admin_portal#user_lookup action's view
create app/views/admin_portal/user_select_action.html.erb - admin_portal#user_select_action action's view
invoke test_unit
create test/controllers/admin_portal_controller_test.rb - test file for the controller
invoke helper
create app/helpers/admin_portal_helper.rb - helper for the controller
invoke test_unit
invoke assets
invoke coffee
create app/assets/javascripts/admin_portal.coffee - coffescript file for the controller's view
invoke scss
create app/assets/stylesheets/admin_portal.scss - styles file for the controller's view
This commands all of the files you would need in classic rails to add styles, javascript (coffeescript), and the views and any helpers in the controller. In this case we won't be using the helpers or the test file, so we can rerun this command and have it remove those files. We must keep the styling file because in our app we are always including the styles for a given controller (see app/views/layouts/application.html.erb:12)
git clean -f -d && git reset --hard
rails generate controller AdminPortal user_report user_lookup user_select_action --no-helper --no-javascripts
* no way to not generate the test file that i found, probably could though
start server
rails s
Go to localhost:3000/admin_portal/user_report
Shows that it works. Now add this form into the user_lookup.html.erb file
<form action="user_select_action">
<input name="user_email" type="email">
<input type="submit">
</form>
This will submit the form to the controller to action user_select_action. Enter this into the admin_portal#user_select_action controller:
def user_select_action
@user = User.find_by(email: params[:user_email])
end
Then add this to the user_select_action.html.erb file:
<form action="user_report">
<label for="user_report"><input name="user_report" type="checkbox">Device Status</label>
<input type="submit">
</form>
Then add the report code to your action and have it return the information in the user_report.html.erb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment