好的,这里的关键是,“其他页面”可能是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>
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>