使用Twitter中的Twitter Bootstrap jquery模式链接到另一个页面

Tim*_*Tim 3 jquery ruby-on-rails twitter-bootstrap

我只是希望这个模态出现并显示另一个页面的内容,但我不能让它工作,因为我是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>
Run Code Online (Sandbox Code Playgroud)

Tom*_*son 6

好的,所以这里的诀窍是"其他页面"可能是Rails控制器操作的结果,通常会将页面呈现在单独的URL上,对吧?然而,bootstrap假设当您单击该链接时,其HTML已存在于当前页面上.

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

编辑:添加具体的例子,在我的情况下,在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>
Run Code Online (Sandbox Code Playgroud)

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

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

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

<li><%= link_to "Edit Account", edit_user_registration_path, :remote => true %></li>
Run Code Online (Sandbox Code Playgroud)