[英]How can I use buttons to dynamically change a form?
I have a table that displays all of a users Teacher models. 我有一个表,显示所有用户的教师模型。 I want the rightmost column of the table to contain 'edit' buttons that cause a bootstrap modal to appear with a form that submits to that models edit path.
我希望表格的最右边一栏包含“编辑”按钮,这些按钮会导致引导模态与提交给该模型编辑路径的表单一起出现。
So far I have a table with buttons whose id's correspond to the id's of the model they are displaying. 到目前为止,我有一个带有按钮的表,这些按钮的ID对应于它们正在显示的模型的ID。 When the buttons are clicked, the modal appears, but the form always submits to the same url.
单击按钮时,将出现模式,但表单始终提交相同的URL。 I'm having trouble finding a way to change the url that the form submits to based on the id of the button that was pressed.
我无法找到一种方法来根据按下的按钮的ID更改表单提交的url。
Button: 按键:
<a id="edit-#{teacher_id_here}" href="#myModal" role="button" class="btn" data-toggle="modal">
Launch demo modal
</a>
Modal: 模态:
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog"
aria-labelledby="myModalLabel" aria-hidden="true">
...
</div>
One possibility is to use a custom data
attribute (eg data-teacher_id
) in your anchor tag and then retrieve the teacher_id
via a callback bound to the click event of the anchor tag. 一种可能性是在锚标记中使用自定义
data
属性(例如data-teacher_id
),然后通过绑定到锚标记的click事件的回调来获取teacher_id
。 The function of the callback would be to update the form in the modal with the extracted teacher_id
(see the documentation on jQuery.data() ) 回调的函数,是更新与所提取的模态形式
teacher_id
(见文档jQuery.data() )
However, in my own code I use a different approach. 但是,在我自己的代码中,我使用了不同的方法。 I would convert to anchor tag to an "ajaxified" link and fill the
#myModal
with the form partial returned by the edit action of the TeacherController. 我将锚标记转换为“ ajaxified”链接,并用
#myModal
的edit操作返回的部分表单填充#myModal。 In that case your myModal
division would initially be empty. 在这种情况下,您的
myModal
分区最初将为空。
The link is set up to point to the right URL and to be handled by Ajax: 链接设置为指向正确的URL,并由Ajax处理:
= link_to "Launch modal", edit_teacher_path(@teacher), :class => "btn ajax-modal-trigger",
:data => {:remote => true, :type => 'html'}
Since the link has the data-remote
attribute set to true, Rails will automatically submit it via ajax to the right URL (namely, the edit_teacher_path(@teacher)
). 由于链接的
data-remote
属性设置为true,因此Rails将通过ajax自动将其提交到正确的URL(即edit_teacher_path(@teacher)
)。 Now you only need to bind the ajax success event to actually fill the modal with the form and to trigger the modal show: 现在,您只需要绑定ajax成功事件即可以表单实际填充模式并触发模式显示:
$("body").on('ajax:success', "a.ajax-modal-trigger", function(evt, data, status, xhr){
$("#myModal").html(data).modal('show');
});
Of course, in the TeachersController's edit
action you need to make sure you handle Ajax requests and respond with the correct form partial (Note that the format is html
even though it's an Ajax request. That's because you want to render an html partial): 当然,在TeachersController的
edit
操作中,您需要确保处理Ajax请求并以正确的形式partial进行响应(请注意,即使它是Ajax请求,其格式也是html
。这是因为您要呈现html局部):
class TeachersContoller
.....
def edit
@teacher = Teacher.find(parame[:id])
respond_to do |format|
format.html do
render :partial => "form", :object => @teacher
end
end
end
end
You could use this as a general-purpose approach whenever you want to render dynamic modals. 每当您要渲染动态模态时,都可以将其用作通用方法。 The only thing that needs to change is the url that the anchor tag points to.
唯一需要更改的是定位标记指向的网址。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.