Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Javascript to Add/Remove Users in Rails Admin Interface
    primarykey
    data
    text
    <p>In a Ruby on Rails application, I want to be able to place a User's username in a input text box, press an 'Add' button, and have them appear underneath with their details. Then, I can simply remove them from the list if I want using another button.</p> <p>How does one connect Javascript and Rails database to complete such a task specifically with those buttons? While Javascript isn't a strength of mine, I'm more puzzled by how to extract and modify the Rails database using Javascript. For reference, I'm also using MongoDB.</p> <p>What would be the best way to approach this?</p> <p><img src="https://i.stack.imgur.com/Xdem3.png" alt="Popular::User UI Add/Remove"></p> <p>Here is the jQuery and AJAX code that I'm using to 'POST' to the server endpoint 'admin/popular/users.json', but I'm not sure how to get Rails to create a new user in the database using my Popular::User model.</p> <pre><code>$(document).ready(function() { $('.add-to-popular-users-button').click(function(event){ event.preventDefault(); var addToPopularUsersBtn = $(this); var userToBeAdded = $('input[name=popular_user]').val(); var data = { 'popular_user': {'username': userToBeAdded, 'category': 'popular'} }; var url = "/admin/popular/users.json"; $.ajax({ url: url, data: data, dataType: 'json', type: 'POST', success: function(e) { alert('Great success!'); } }); }); }); </code></pre> <p>Here's my Popular::User model:</p> <pre><code>class Popular::User include Mongoid::Document include Mongoid::Timestamps POPULAR = 'popular' field :category, default: POPULAR index :user_id belongs_to :user validates_presence_of :user_id validates_uniqueness_of :user_id def self.popular user_ids = self.where( :category =&gt; POPULAR ).map(&amp;:id) User.where(:_id.in =&gt; user_ids) end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload