1

I have the controller action:

class UsersController < ApplicationController

  def new
    @user = User.new
    if params[:email]
      @email = params[:email]
      @user.email = @email
    end
  end

  def create
    @user = User.new(user_params)
    @user.save
    redirect_to root_path
  end

  private

  def user_params
    params.require(:user).permit(:name, :email)
  end
end

and this is the form in my footer (which is working since its passing the params) -> the button-div is bootstrap:

        <%= form_tag new_user_path, class: "email-control", method: :get do %>
        <%= text_field_tag :email,
          params[:email],
          class: "email-bar",
          placeholder: "Type your email..."
        %>
        <div class="input-group-append">
          <button type="submit" class="button-sent" data-bs-toggle="modal" data-bs-target="#login-popup"><i class="far fa-paper-plane"></i></i></button>
        </div>
        <% end %>

BUT the following simple_form should be a MODAL appearing on every page I type in my email in the footer:

<div class="login-container">
  <div class="row center-div">
    <div class="col-xs-12 col-sm-4 col-sm-offset-4">
      <div class="form-login">
        <h2>Send me Newsletters</h2>
        <%= simple_form_for(@user) do |f| %>
          <div class="form-inputs">
            <%= f.input :name, required: false, autofocus: true %>
            <%= f.input :email, required: false %>
          </div>
          <div class="form-actions">
            <%= f.button :submit, "⤏Submit⤎", class: "email-control " %>
          </div>
        <% end %>
      </div>
    </div>
  </div>
</div>

When following this page: https://kolosek.com/rails-creating-modals/ I was able to see this: modal but its not able to renderI guess the JS is not working? I also think the issues is that my controller action can only be red by the new.html.erb view and can therefore not be reached by any other page? Thank you so much for helping.

Witta
  • 15
  • 5
  • Hey Witta, could you please elaborate a bit more on: What is happening? And what is not happening? I am unsure if the modal is not appearing at all or if it is not working? :) – tomr Jan 03 '21 at 23:06
  • The button that should toggle your modal is inside the form for a new user. So when you click that button it will submit the form and redirect you so your modal will be closed immediately. Instead of using a form you can just link to the `new_user_path` with remote true. – Hackman Jan 04 '21 at 07:55
  • @Hackman but when changing form_tag to link_to the :email params wont get passed. Is there a way to get the modal opening up and the email is already pre-filled? – Witta Jan 04 '21 at 09:25
  • @tomr Hi there, thanks for answering! Bootstrap is not working at all but when I follow this page (see edited question above) the problem is that I am not able to render the new.html.erb since its not a partial (but the view from user new action). Is that more clear now? thx – Witta Jan 04 '21 at 09:25
  • You can do that with some javascript. On modal open you grab the email field value and put it in the modal email field. – Hackman Jan 04 '21 at 09:29

0 Answers0