jquery - 在rails中使用Twitter Bootstrap jquery modal链接到另一个页面

标签 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>

最佳答案

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

那么该如何处理呢?您需要通过 AJAX 发出 Controller 请求(link_to ... :remote => true 是一个很好的起点)并更改 Controller 以将其内容添加到当前的 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>

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>

关于jquery - 在rails中使用Twitter Bootstrap jquery modal链接到另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10318949/

相关文章:

javascript - 单击按钮通过 Ajax 请求将 Javascript 数组发送到 Rails Controller

html - 搜索框 bootstrap 4 无法定位在导航栏的中间

javascript - 使用 jquery 保存动态添加的内容

javascript - 将输入限制为一组字符,同时显示这些字符

javascript - 通过循环索引获取数据属性

ruby-on-rails - 设计在 authenticate_user 时松开 i18n.locale!开始

javascript - 如果 rtl,则更改焦点处的输入文本语言

ruby-on-rails - 日期范围的自定义 ActiveAdmin 过滤器

html - Bootstrap 从一个网格大小列更改为另一个

twitter-bootstrap - Twitter Bootstrap 3.1.1 navbar-right 溢出导航栏