我有一个显示所有可用帖子的posts_controller
。每个帖子都有link_to
:request => true
,允许用户向其他用户推荐给定的帖子。
当用户点击链接时,会出现一个模式(通过AJAX),该模式会呈现推荐索引页面。模态内的索引页面由搜索字段组成。当用户输入他们想要与之共享帖子的用户的名字,并点击提交按钮时,我希望使用返回的搜索结果更新模态。相反,目前,该页面重新路由到post_recommendations_url
(.. / posts / id / recommendations?search_criteria)。
我希望搜索结果在模态中更新,而不是重定向到post_recommendations_url。有没有办法实现这一目标?
这是我的控制器和视图的样子:
Index.html.haml(位于users / posts文件夹中)
%li
= link_to 'Recommend post', post_recommendations_path(post), :remote => true, "data-toggle" => "modal"
#my-modal.modal.hide
.modal-header
%a.close{"data-dismiss" => "modal"} ×
%h6
%i.icon-share.icon-large
Recommend Project
.modal-body
#modal-rec-body
%p
* recommendations_controller.rb *
def index
@user = Search.find_user(params[:name], current_profile)
respond_to do |format|
format.html
format.js
end
end
index.js.haml(位于推荐文件夹中)
:plain
$("#modal-rec-body").html("#{escape_javascript(render('recommendations/recommendation'))}");
$('#my-modal').modal({
keyboard: true,
show: true
});
* _推荐部分(位于推荐文件夹中)*
.row-fluid{:style => "background:#ffffff; margin-left:0px"}
.span12
= form_tag post_recommendations_path, :method => "get" do
= text_field_tag :name, '', :class => "span12", :placeholder => "Please enter the name of the user you would like to share this post with.", :style => "max-width:520px;"
%center
= submit_tag "Search", :class => "btn btn-primary", :remote => "true"
- @user.each do |i|
- unless current_profile == i
.row-fluid
.span6
.row-fluid
.well{:style => "margin-left:0px;"}
.row-fluid
.span2
=image_tag i.avatar(:bio), :class=> "sidebar_avatar"
.span4
- form_for :recommendation do |r|
= r.hidden_field :friend_id, :value => i.account.id
= r.submit "Send Recommendation", :class => "btn btn-primary"