我不明白这个 Rails 模板渲染的行为
我有这个控制器:
class FormCreatorController
def build_form
if param[:ftype] == 2
@partial_file = "extra"
else
@partial_file = "basic"
end
respond_to do |format|
format.js
end
end
end
这是从 new
视图调用的,其中代码如下:
<div class="column span-16 last">
<%= form_tag build_form_ads_path,:method => :get,:remote => true,:id => "ad_builder_form" do %>
<div class="column span-4">
<strong>Form type</strong>
</div>
<div class="column span-12 last">
<%= select_tag(:ad_type,options_for_select(@ad_types_mapped)) %>
</div>
<% end %>
<div class="column span-16 last" id="myform">
Please select an option
</div>
</div>
我使用 jQuery 作为我的 ajax 框架。 myform
div,ideally
应替换为从 build_form 返回的内容。我在 build_form.js.erb
中有这个:
$('#myform').html($('<%= render :partial => @partial_file %>'))
我的基本表单包含这个:
<h1>Basic</h1>
我的 extra
表单包含这个:
<h2>Extra</h2>
但是,当发出 XHR 请求时,我的表单没有任何反应分区但是,如果我将其放入我的部分中:
$('#myform').html("<h1>Basic</h1>")
然后
$("#myform").html("<h1>Car</h1>");
它们会正确显示在页面中。使用 javascript 字符串连接构建自定义表单很乏味,但可行。我确信有更好的做事方式。我应该怎么做才能正确渲染我的部分代码而不是我的 div?
编辑:也许有帮助。这是我的 application.js 的内容:
$().ready(function() {
jQuery.ajaxSetup({
'beforeSend': function(xhr) {
xhr.setRequestHeader("Accept", "text/javascript");
}
});
$("#ad_type").change(function() {
$("#ad_builder_form").submit();
});
});
每次选择的值发生变化时,我都会使用 jQuery 提交表单。
I have this controller:
class FormCreatorController
def build_form
if param[:ftype] == 2
@partial_file = "extra"
else
@partial_file = "basic"
end
respond_to do |format|
format.js
end
end
end
This is called from a new
view, where the code is like this:
<div class="column span-16 last">
<%= form_tag build_form_ads_path,:method => :get,:remote => true,:id => "ad_builder_form" do %>
<div class="column span-4">
<strong>Form type</strong>
</div>
<div class="column span-12 last">
<%= select_tag(:ad_type,options_for_select(@ad_types_mapped)) %>
</div>
<% end %>
<div class="column span-16 last" id="myform">
Please select an option
</div>
</div>
I am using jQuery as my ajax framework.
The myform
div, ideally
should be replace with the contents of what's returned from the build_form. I have this in the build_form.js.erb
:
$('#myform').html($('<%= render :partial => @partial_file %>'))
My basic form contains this:
<h1>Basic</h1>
My extra
form contains this:
<h2>Extra</h2>
But, when the XHR request is made, nothing happens to my div. If, however, I put this in my partials:
$('#myform').html("<h1>Basic</h1>")
and
$("#myform").html("<h1>Car</h1>");
then they are correctly displayed in the page. Building custom forms using javascript string concatenation is tedious, but doable. I am sure there is a better way of doing things. What should I do so that the code from my partials is correctly rendered instead of my div?
EDIT: maybe it helps. This is the content of my application.js:
$().ready(function() {
jQuery.ajaxSetup({
'beforeSend': function(xhr) {
xhr.setRequestHeader("Accept", "text/javascript");
}
});
$("#ad_type").change(function() {
$("#ad_builder_form").submit();
});
});
Every time the value of the select changes, I'm submitting the form with jQuery.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以返回 html update:
然后更新客户端的元素。
或者,为了防止动态添加 html 出现问题,请使用 jquery 的 live:
然后像以前一样从应用程序返回脚本:
You can return the html update:
then update the element client side.
or, to prevent problems with dynamically added html use jquery's live:
then return the script from your application as before: