Trying to make a form in a jQuery dialog modal-for

2019-08-24 04:49发布

I want my views/blogs/new to open in a dialog. Tried to make it work per this SO answer: https://stackoverflow.com/a/5318155/732200

Here is my code (the blogs are called "fables"):

This is my partial - views/fables/fablemaker.html.erb

<div id="fableMaker" title="Fable Maker" style="display:none">

<div id="countdownTimer">
    <span id="countdown_timer" class="timer">4:20</span><br />
    <button id="play_btn">Play Timer</button>
    <button id="pause_btn">Pause Timer</button>
    <button id="reset_btn">Reset Timer</button>

</div>

<%= form_for @fable do |f| %>

  <p>
    <%= f.label :content, "Fable Portal:" %><br />
    <%= f.text_area :content, :style => "" %>
  </p>
  <p>
    <%= f.label :title, "What would you like to title your creation?" %><br />
    <%= f.text_field :title %>
  </p>
      <p><%= f.submit "Create Fable", remote: true %></p>
    <% end %>

    </div>

Here is the views/fables/create.js.erb:

$("#fableMaker").dialog({
    autoOpen: false,
    height: 900,
    width: 550,
    modal: true,
    title: 'Fable Maker',
    buttons: {
       "Create": function() { $("#fable_form").submit() },
      },
      open: function() {
        $("#fable_form").html("<%= escape_javascript(render('fablemaker')) %>")
    },
});

Here is the fables_controller, create action:

class FablesController < ApplicationController
  respond_to :html, :js

  def create
    @user  = current_user
    @fable = @user.fables.build(params[:fable])
    @fable.save
    respond_with @fable, :location => @fable
  end

Here are the related routes calls:

  root :to => "pages#home"

  resources :fables

  match '/views/fables/fablemaker', :to => 'fables#create'
  get "fables/create"

This is the link that is supposed to launch the page:

  <li><%= link_to image_tag("makeyourfable.png", :size => "130x102", 
                                                 :alt => "freewriting portal", 
                                                 :class => "typewriter", 
                                                 :mouseover => "makeyourfableH.png"),
                                                 new_fable_path %>
  </li>

When I check the page source, all of the elements and fields are loading, but they are not visible - and not, of course, in a dialog box. All that shows is my application.html content.

1条回答
虎瘦雄心在
2楼-- · 2019-08-24 05:49

In the open function of the dialog jQuery, you're trying to insert new HTML into a #fable_form element.

I believe you meant to insert that HTML into #fableMaker element.

That js response is rendering the html that will make up the form and inserting into a div you already had in the calling view.

$("#fableMaker").dialog({
    autoOpen: false,
    height: 900,
    width: 550,
    modal: true,
    title: 'Fable Maker',
    buttons: {
       "Create": function() { $("#fable_form").submit() },
      },
      open: function() {
        $("#fableMaker").html("<%= escape_javascript(render('fablemaker')) %>")
    },
});
查看更多
登录 后发表回答