3

我只是想让这个模式出现并显示另一个页面的内容,但我无法让它工作,因为我是 Jquery 的新手。有没有办法修改下面的代码,以便在模式弹出窗口中显示“/contact/”的内容?

----------
# this content is on http://myurl.com/contact/
 <div style="display: none;" id="myModal" class="modal hide fade">
 <p>this is my email app here...</p>
</div>
----------
# this is the button on a 'show' page where i want someone to push to bring up the contents of the above page in the modal.
<a data-toggle="modal" href="#myModal" class="btn btn-primary btn-large">Launch Email</a>
4

1 回答 1

7

好的,所以这里的技巧是“其他页面”可能是 Rails 控制器操作的结果,该操作通常会在单独的 URL 上呈现页面,对吧?然而,bootstrap 假定当您单击该链接时,它的 HTML 已经存在于当前页面上。

那么如何处理呢?您需要通过 AJAX 发出控制器请求(link_to ... :remote => true这是一个很好的起点)并更改控制器以将其内容添加到当前页面某处的 div 中。完成后,您上面的代码或类似的代码(或 Bootstrap 页面上记录的代码)将执行您想要的操作。

编辑:添加特定示例,在我的情况下,在引导模式中打开编辑用户页面(使用 Devise gem 进行身份验证):

app/views/devise/registrations/_edit.html.erb:

<div class="modal hide fade in" id="user-edit">
  <div class="modal-header">
    <button class="close" data-dismiss="modal">x</button>
    <h2>Edit User</h2>
  </div>

  <%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => {:method => :put}) do |f| %>
    <div class="modal-body">
      <%= devise_error_messages! %>

      <div>
        <%= f.label :name %><br/>
        <%= f.text_field :name %>
      </div>

      <div>
         <%= f.label :email %><br/>
        <%= f.email_field :email %>
      </div>

      <div>
        <%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br/>
        <%= f.password_field :password, :autocomplete => "off" %>
      </div>

      <div>
        <%= f.label :password_confirmation %><br/>
        <%= f.password_field :password_confirmation %>
      </div>

      <div>
        <%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br/>
        <%= f.password_field :current_password %>
      </div>
    </div>

    <div class="modal-footer">
      <%= f.button "Update", :class => "btn btn-primary", :data => { :dismiss => "modal"} %>
      <a href="#" class="btn" data-dismiss="modal">Close</a>
    </div>
  <% end %>
</div>

app/views/devise/registrations/edit.js.erb:

$("#main").before("<%= escape_javascript(render "edit", :formats => [:html]) %>");
$("#user-edit").modal();

并且,从任何页面调用它的链接(我现在在导航中有它:

<li><%= link_to "Edit Account", edit_user_registration_path, :remote => true %></li>
于 2012-04-25T20:51:20.253 回答