0

I'm trying to get a modal to open to _form.html.erb and submit data. I'm having a problem with even showing the modal. I have rail 4 installed with bootstrap 3. I have been using the following for the example code..

How to add bootstrap modal with link_to so the link content open in modal ?

Code I have in index.html.erb where I have the button for the modal to open:

<!-- Begin Modal Test -->
<%= link_to "Open modal", new_weight_path, :class => "btn btn-default", :remote => true, "data-toggle" => "modal", "data-target" => "my-modal" %>
<div class="modal hide fade" id="my-modal" title="My modal">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button aria-hidden="true" class="close" data-dismiss="modal" type="button">×</button>
        <h3 id="myModalLabel">Add New Measurement:</h3>
      </div>
      <div class="modal-body a-unique-class">
        New Measurements
      </div>
      <div class="modal-footer">
        <button aria-hidden="true" class="btn" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>
<!-- Modal Test -->

JS in new.js.erb:

$(".a-unique-class").html('<%= j render "posts/_form" ')

I know I need to rename my classes and ID's, but I am just trying to get this to popup first. Am I missing something for my JS file or modal?

In the rails console I see the following:

Started GET "/weights/new

Rendered weights/_form.html.erb (11.5ms)

Rendered weights/new.html.erb within layouts/application (15.1ms)

So to me, it looks like is at least making the call, just no popup.

I have tried this within index.html.erb, but all it does it make the screen dim and no popup.

    <%= link_to "Open modal", "#my-modal", :class => "btn", "data-toggle" => "modal" %>
<div class="modal hide fade" id="my-modal" title="My modal">
  <div class="modal-header">
    <button aria-hidden="true" class="close" data-dismiss="modal" type="button">×</button>
    <h3 id="myModalLabel">Modal header</h3>
  </div>
  <div class="modal-body">
    Modal Body
  </div>
  <div class="modal-footer">
    <button aria-hidden="true" class="btn" data-dismiss="modal">Close</button>
  </div>
</div>

Thanks!

** Update **

I have gotten the modal to display with the below code..

    <%= link_to "Add Measure Modal", new_weight_path, :class => "btn btn-default", :remote => true, "data-toggle" => "modal", "data-target" => "#myModal" %>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
      <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">Add New:</h4>
        </div> <!-- End Modal Header -->
        <div class="modal-body addmeasurement">
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          <button type="button" class="btn btn-primary">Save Changes</button>
        </div>
      </div> <!-- End Modal Content -->
    </div> <!-- End Modal Dialog -->
  </div>

JS File:

$(".addmeasurement").html('<%= j render "weights/_form" %>')

My problem now is, the _form is not rendering into the modal. I'm missing a step or something in my controller?

Guess this might also be helpful, my controller:

  def create
  @weight = Weight.new(weight_params)
  flash[:notice] = "Measurements Added Successfully"
  @weight.save
  redirect_to :action => 'index'
  end

************* Update **************** Alright, so per the suggestion below.. I can now get my modal to open and render sometimes, well at least render text. The Form keeps throwing errors at me. I am using the _modal_form.html.erb file for this now as well.

The form I have:

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

      <ul>
      <% @weight.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

However, when I keep it like this, it throws a NIL error on the form. I can fix this by doing the following:

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

But when doing this, I can't actually insert any values. I guess, why doesn't my controller already pass @weight over? below is my create definition:

  def create
  @weight = Weight.new(weight_params)
  flash[:notice] = "Measurements Added Successfully"
  @weight.save
  redirect_to :action => 'index'
  end

************** Update for Edit Modal

So this is what I have for the Edit modal..

_modal_edit_form.html.erb:

<%= form_for(@weight) do |f| %>

weight_controller.rb:

  def edit
  @weight = Weight.find(params[:id])
  end

  def update
  @weight.update(weight_params)
  flash[:notice] = "Measurements Updated Successfully"
  redirect_to :action => 'index'
  end

I can edit it properly through a direct link /weight/130/edit for example. That pulls the form up and autofills with the properties, but won't through the Modal.

Per your comment, I thought this is what was needed? However, no luck.. Doesn't do anything. Sorry, I'm still relatively new to RoR and trying to get the understanding of the controller and how it calls methods. Thanks!

Community
  • 1
  • 1
jwg88
  • 50
  • 10

1 Answers1

0

This what I use on my modal and works fine. the button part you can use the link_to if you want. I hope this give you a idea on how to show the form in the modal. Then you can add some js for clearing, validation and closing after submit.

-- link_to or button --

  <button class="btn btn-success" data-toggle="modal" data-target="#newInspectionModal">
    <i class="icon-plus icon-white"></i>
  </button>

-- Modal --

  <div class="modal fade" id="newInspectionModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
          <h4 class="modal-title" id="myModalLabel">Nueva Inspección</h4>
        </div>
        <div class="modal-body">
            <%= render :partial => "modal_form" %>
        </div>
      </div>
    </div>
  </div>

And the partial
_modal_form.html.erb

 <%= bootstrap_form_for @inspection,remote:true, :url => project_inspections_path(@project) do |f| %>
   <%= f.text_field :name, label:"Name" %>
   <%= f.submit "Crear", :class => 'btn btn-success'%>
  <% end %> 

If your creating the form with out the new.html.erb you need to pass the @weight on your index.
Update:

def new
    @weight = Weight.new
  end

  def new
    @weight = Weight.new
  end

  def edit
    @weight = Weight.new
  end
Frank004
  • 228
  • 1
  • 2
  • 16
  • Thanks! I'm trying to get this implemented.. But I am having a problem. I am trying to just include the _form.html.erb. This might not be ideal. But for now I am just trying to get it to work with the default form. However.. When I do a render partial for form I get "First argument in form cannot contain nil or be empty". The first line of the form is: <%= form_for(@weight) do |f| %>. Any thoughts why it would not like the default form? – jwg88 Jan 03 '15 at 19:34
  • Can you confirm that `@weight` is being set to something and is not `nil` at the time it is passed into the `form_for` method? – Rob Wise Jan 03 '15 at 20:26
  • It works when I don't use the modal and it goes through the standard new.html.erb. Is there a way I can see or verify that? – jwg88 Jan 03 '15 at 20:33
  • Ahh.. Is it because I am rendering the partial modal_form and not the new.html.erb? But render "new" doesn't work the way I would think? – jwg88 Jan 03 '15 at 21:55
  • on the controller you pass in the new method @weight. you need to pass the same in the index if is where your using the modal ``` def new @weight = Weight.new end def new @weight = Weight.new end def edit @weight = Weight.new end ``` – Frank004 Jan 04 '15 at 22:53
  • Hmm One more question. I am trying to add this so I can edit each entry. So I have a table that displays the weights from the DB. Trying to create a new modal #editMeasurement and when I click the button, it does what the update def would do and open the _modal_edit_form with the values in the text boxes. I can get it to render the form fine. But its loading a blank form like add/create would do. Any idea? – jwg88 Jan 05 '15 at 06:07
  • Link/Button <%= link_to edit_weight_path(weight), :remote => true, "data-toggle" => "modal", "data-target" => "#editMeasurement" :class => "btn btn-default", 'aria-label' => "Edit" do %><% end %> – jwg88 Jan 05 '15 at 06:10
  • are you using the form_for(@weight) on the edit form. You need to have the same instant variable that has the values that you create with ```@weight. @weight = weight.find(params[:id]) if your calling on edit add this to edit method hope this helps ``` – Frank004 Jan 05 '15 at 14:12
  • I edited my post with the code I am trying. Still no luck. Not sure why, unless I am not doing the form_for right? – jwg88 Jan 06 '15 at 22:54
  • I can also see it access the right ID in the console too.. The following: CACHE (0.0ms) SELECT `weights`.* FROM `weights` WHERE `weights`.`id` = 133 LIMIT 1 [["id", "133"]] It also does the same query when doing an edit through the direct URL and not modal. – jwg88 Jan 07 '15 at 00:32
  • So.. Messing around with it, I tried to put the modal (editmodal) into the loop. I saw some tutorial that did this, but cannot find the link anymore. And I create a new call for each edit button / modal.. editModal_#{weight.id} etc.. However, it screws up my whole index render and doesnt put it into a table. – jwg88 Jan 08 '15 at 01:23