2

So I am having an issue creating a user in a modal from my navbar. I want to add the link to allow an administrator to create a new user from a link as opposed to going to users index and use the modal button there.

My nav-bar link:

 <li><%= link_to "Add User", new_user_path, remote: true %></li>

My Modal so far:

found in: _create.html.erb

<%= content_tag :div, class: "modal fade", id: "createUser" do %>
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <%= form_for(@user, remote: true) do |f| %>
        <div class="modal-body">
          <div class="row">
            <div class="col-xs-12">
               FORM HERE
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          <%= f.submit "Create New User", :class => 'btn btn-primary' %>
        </div>
      <% end %>
    </div>
  </div>
</div>

my create.js.erb

$('#createUser').modal('hide');

$(".email").val('');
$(".password").val('');
$(".f_name").val('');
$(".l_name").val('');
$(".primary_tel").val('');

$('#table_body').prepend('<%= j render partial: 'user_row', locals: {user: @user} %>');
$('#user_row_<%= @user.id %>').hide().fadeIn(1000);

users controller / new action

  def new
    @user = User.new
  end

User form I want to turn into a modal:

<%= form_for(user) do |f| %>
  <% if user.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:</h2>

      <ul>
      <% user.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
  <div class="field">
    <%= f.label :email %><br />
    <%= f.email_field :email, autofocus: true %>
  </div>

  <div class="field">
    <%= f.label :password %>
    <% if @minimum_password_length %>
    <em>(<%= @minimum_password_length %> characters minimum)</em>
    <% end %><br />
    <%= f.password_field :password, autocomplete: "off" %>
  </div>

  <div class="field">
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation, autocomplete: "off" %>
  </div>

  <div class="field">
    <%= f.label :user_ident %>
    <%= f.text_field :user_ident %>
  </div>

  <div class="field">
    <%= f.label :f_name %>
    <%= f.text_field :f_name %>
  </div>

  <div class="field">
    <%= f.label :l_name %>
    <%= f.text_field :l_name %>
  </div>

  <div class="field">
    <%= f.label :primary_tel %>
    <%= f.text_field :primary_tel %>
  </div>

  <div class="field">
    <%= f.label :role_id %>
    <%= collection_select(:user, :role_id, Role.all, :id, :name, {prompt: true}) %>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
Shawn Wilson
  • 1,311
  • 14
  • 40
  • Can you explain what error you're getting? – Arun Kumar Mohan Aug 30 '16 at 05:01
  • @ArunKumar There is no error at all, it just wont load or display the modal.. pretty much like a dead link. – Shawn Wilson Aug 30 '16 at 05:07
  • Can you post the code in `new` action of `UsersController` and in `new.js.erb`? – Arun Kumar Mohan Aug 30 '16 at 05:08
  • I have the _create.js.erb ill try renaming it to new and add a format block to new aswell – Shawn Wilson Aug 30 '16 at 05:11
  • Post the code of the view you're rendering for the `new` action – Arun Kumar Mohan Aug 30 '16 at 05:13
  • its actually the create action i want to use for this, however there is no user_create path ? – Shawn Wilson Aug 30 '16 at 05:16
  • i essentially want to put my user create form in a modal i can access from the nav bar – Shawn Wilson Aug 30 '16 at 05:16
  • @ArunKumar, im not sure what else you need here? my goal is to turn the user create form into a modal that the admin can access from the navbar at any time. My original thought process was to put it under the new action but thinking about it now it makes no sense to me.. it should stay under the create action – Shawn Wilson Aug 30 '16 at 05:20
  • I am adding my answer. In the meanwhile, to understand the workflow of AJAX in rails, please read my answer [here](http://stackoverflow.com/questions/38875347/render-three-different-partials-depending-on-button-clicked/38939134#38939134) P.S. I am not advertising. – Arun Kumar Mohan Aug 30 '16 at 05:24

1 Answers1

1

I assume you want to display the modal in the index view of UsersController. Add your code in _create.html.erb to the top of users/index.html.erb. Your modal won't display unless you explicitly ask it to. Since you're rendering a form in the modal for @user, you need to define what @user is in index action.

# users_controller.rb

def index
  @users = User.all
  @user = User.new

end

You have specified remote:true in your link to new_user_path, you have to render a js response in the new action.

def new
  @user = User.new
  respond_to do |format|
    format.js #Looks for `users/new.js.erb`
    #other formats
  end
end

Since we already have the modal template in index.html.erb, all you need to do in new.js.erb is to display the modal.

# new.js.erb

$("#createUser").modal('show');

Since you have create.js.erb ready, thats all to be done.

EDIT: If you want to place this link in the navigation bar, you should add the modal template to a partial(say layouts/_new_user_modal.html.erb) and render it in application.html.erb which is common to all the views. Also, you need to make a change to your form. Instead of

<%= form_for(@user, remote: true) do |f| %>

use

<%= form_for(User.new, remote: true) do |f| %>

This way you don't have to define @user in every action of your application (DRY).

Hope this helps!

Arun Kumar Mohan
  • 11,517
  • 3
  • 23
  • 44
  • Ok, so I actually want to display the modal anywhere in the application. its working in the users/index page now, however i would like to be able to just go and hit the link and pop the modal anywhere i am in the app.. is this doable? – Shawn Wilson Aug 30 '16 at 05:40
  • @ShawnWilson, Updated my answer. Does this help? – Arun Kumar Mohan Aug 30 '16 at 05:44
  • this is Fantastic! thanks much! And so you know im going through the article you wrote! Fantastic read and ill def keep on learning my AJAX and JS! – Shawn Wilson Aug 30 '16 at 05:48
  • @ShawnWilson Thats a long read. Thanks for reading. Glad that it helped. – Arun Kumar Mohan Aug 30 '16 at 05:49