0

I have a profile edit page and would like to have two modals within the edit form. One modal will open a newsletter preference section of the form (as seen below) The other modal will open a password field to enter and save the changes. Both modals are inside the <% end %> tags of the form.

They didn't open, changed some code, now undefined local variable or method 'f' inside _passwordModal.html.erb

Edit page html:

<div class="wellington center">
<%= form_for :users, url: user_path(@user), method: :patch do |f| %>

<div class="row">
  <div class="col-md-6 left">
    <div class="form-group">
      <%= f.text_field :username, :required => false, disabled: true, placeholder: "#{@user.username}", class: 'form-control', id: 'editUsername' %>
      <%= f.text_field :first_last_name, placeholder: "#{@user.first_last_name}", class: 'form-control' %>
      <%= f.email_field :email, placeholder: "#{@user.email}", class: 'form-control' %> <!-- id: "inputEmail" -->
    </div>
    <div class="news-pref">
      <a href="#mailPref" role="button" class="btn btn-xs btn-warning" data-toggle="modal">Email Preferences</a>
    </div>
  </div>

  <div class="col-md-6 left">
    <label id="outlineLabel" for="birthday">Birth Date</label>
    <form class="form-inline" id="birthday">
      <div class="form-group">
        <input type="text" class="form-control" id="inputMonth" placeholder="Month">
      </div>
    </form>
    <form class="form-inline">
      <div class="form-group">
        <input type="text" class="form-control" id="inputDay" placeholder="Day">
      </div>
      <div class="form-group">
        <input type="text" class="form-control" id="inputYear" placeholder="Year">
      </div>
    </form>

    <label id="outlineLabel" for="country">Location</label>
    <form class="form-inline" id="country">
      <div class="form-group">
        <input type="text" class="form-control" placeholder="Country">
      </div>
      <div class="form-group">
        <input type="text" class="form-control" placeholder="State / Prov. / Region">
      </div>
      <div class="form-group">
        <input type="text" class="form-control" placeholder="City">
      </div>
    </form>
  </div>

</div>
  <a href="#passModal" role="button" class="btn btn-success save" data-toggle="modal">Save Changes</a>
  <% render 'newsPref' %>
  <% render 'passwordModal' %>
  <% end %>
</div>

_newsPref.html.erb view:

<div class="modal hide fade" id="mailPref" tabindex="-1" role="dialog" aria-labelledby="mailPrefLabel">
  <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="mailPrefLabel">Email Preferences</h4>
      </div>
      <div class="modal-body">

        <p><em>How would you like to receive our newsletter?</em></p>
        <div class="checkbox">
          <%= f.label :newsletter, class: "checkbox inline" do %>
            <%= f.check_box :newsletter %>
            <span>By checking this box, you agree to get emails from us, via our newsletter as well as our priority announcements.</span>
          <% end %>
        </div>
        <p>We typically send out a weekly newsletter to provide information on what we uploaded, wrote about, and tips that were all published during the week.</p>
        <div class="checkbox">
          <%= f.label :less_mail, class: "checkbox inline" do %>
            <%= f.check_box :less_mail %>
            <span>1 Newsletter per month: if you feel overwhelmed by weekly updates, don't worry!. By checking this box we'll cut back on the ammount we send to you, but still give you the opportunity to see what happend during the previous month.</span>
          <% end %>
        </div>
        <p><span class="text-muted">Priority anouncements may include sweepstakes, contests or other information that is "time-critial". These announcements are not always included in the newsletter and may be sent in addition to your newsletter preference.</span></p>
        <div class="checkbox">
          <%= f.label :newsletter, class: "checkbox inline" do %>
            <%= f.check_box :newsletter %>
            <span>To opt-out of recieving any future newsletters, go ahead and check this box. We hope to see you come back soon!</span>
          <% end %>
          <p><em>psst... By opting out you might miss out on some sweet discounts, contests and givaways!</em></p>
        </div>

      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-success" data-dismiss="modal" data-toggle="modal" data-target="#passModal">Save Changes</button>
      </div>
    </div>
  </div>
</div>

_passwordModal.html.erb view:

<div class="modal hide fade" id="passModal" tabindex="-1" role="dialog" aria-labelledby="passModalLabel">
  <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="passModalLabel">Confirm Password</h4>
      </div>
      <div class="modal-body">

        <p><em>Please enter your password to confirm changes.</em></p>
        <div class="form-group">
          <%= f.password_field :password, :placeholder => "Password", class: "form-control center" %>
        </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
        <% f.submit "Save changes", class: "btn btn-success" %>
      </div>

    </div>
  </div>
</div>

.JS for swapping modals:

$("#edit-switch").click(function() {
$("#mailPref").modal('hide');
$("#passModal").modal('show');
});
7
  • What do you mean by 'not working'? Do they get rendered, but the modal doesn't open? (That's not my -1 btw. Seems harsh to me.) Commented Mar 19, 2018 at 16:30
  • Well, the modal didn't open the first time I tried it. changed some stuff, now it's saying undefined local variable or method 'f' inside _passwordModal.html.erb Commented Mar 19, 2018 at 16:49
  • 1
    Cool cool - the new problem should be easily solved: just update the render calls to the following: i.e. <% render 'newsPref', f: f %>. Commented Mar 19, 2018 at 16:52
  • Thank you! It now loads the page. The modals are not opening though. I've added my .js for swapping out modals when one is already open, just in case that is what's what would be causing it. Commented Mar 19, 2018 at 16:58
  • 1
    OK - my suggestion would be to create a minimal modal example outside of the form and see whether you can get that working. It sounds like there's either an issue with how your modal / its button's formatted, or that the Bootstrap Javascript isn't loading correctly. My gut says the latter as I can't see anything wrong with the HTML at a glance. Give me a shout if you've any other questions! Commented Mar 19, 2018 at 17:17

1 Answer 1

1

Seems like you are missing the id #edit-switch in your Edit page.html. So the Jquery listener is not triggered. You may also need to use on('click') instead to keep track of the DOM elements required to activate the event listener.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow thanks, I didn't even realize this! I tried it out with & w/o $("#editSwitch").on('click', function() { and it still isn't working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.