[英]How do I get a popup form to pass more details to a controller with Rails 4.1 Bootstrap
I have a button action in a grid that calls an 'Approve' action in the controller. 我在网格中有一个按钮操作,在控制器中调用“批准”操作。 How do I pop an intermediate form to capture a reason that will also get passed to the action?
如何弹出中间表单以捕获也将传递给操作的原因?
I am using Rails 4.1 with Bootstrap. 我正在使用Rails 4.1和Bootstrap。 Happy to use another form, straight JS or bootstrap modals, whatever will work really - just have never been able to get this working cleanly.
很高兴使用另一种形式,直接JS或自举模式,无论什么都可以工作 - 只是从来没有能够干净利落地工作。
route looks like: 路线看起来像:
get 'approve_leave_request/:id' => 'leave_requests#approve', as: :approve_leave_request
controller action: 控制器动作:
def approve
@leave_request = LeaveRequest.find(params[:id])
@leave_request.status = "Approved"
@leave_request.leave_request_actions.build([status: "Approved by #{current_user.name}"])
if @leave_request.save
flash[:success] = "Leave request has been approved."
LeaveRequestMailer.delay.approve_leave_request(@leave_request, ActsAsTenant.current_tenant.full_domain)
else
flash[:error] = "Leave request could not be approved."
end
redirect_to leave_requests_path
end
example button in the view looks like this: 视图中的示例按钮如下所示:
<%= link_to approve_leave_request_path(r), class: "btn btn-success btn-xs" do %>
<span>Approve</span>
<% end %>
You change the link_to
above to a button that toggles the modal: 您将上面的
link_to
更改为切换模式的按钮:
<!-- Button trigger modal -->
<button class="btn btn-success btn-xs" data-toggle="modal" data-target="#approvalModal">
Approve
</button>
And then in the modal you create a small form that passes the reason to the action (in this example, it uses reason
as the parameter, which can be accessed as params[:reason]
in your action: 然后在模态中创建一个小形式,将原因传递给动作(在本例中,它使用
reason
作为参数,可以在动作中作为params[:reason]
访问:
<!-- Modal -->
<div class="modal fade" id="approvalModal" tabindex="-1" role="dialog" aria-labelledby="approvalModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title">Approval</h4>
</div>
<div class="modal-body">
<%= form_tag approve_leave_request_path(r), method: :get, :class => "form-horizontal" do %>
<%= label_tag 'reason', 'Reason for Approval', :class => 'control-label' %>
<%= text_field_tag :reason, params[:reason], :class => "form-control" %>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
<%= submit_tag "Submit Approval", name: nil, class: "btn btn-success" %>
<% end %>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
This creates a modal like so: 这样就创建了一个模态:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.