通过函数传递 javascript,html_safe、raw 和 f 不起作用
我正在关注 Railscasts 196 和 197,以便更好地学习动态嵌套模型形式,并且我可以解决这个问题。
我不知道如何通过 link_to_function 传递 javascript,字段的类包含 jquery timepicker。
def link_to_add_fields(name, f, association)
new_object = f.object.class.reflect_on_association(association).klass.new
fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
render(association.to_s.singularize + "_fields", :f => builder)
end
link_to_function(name, "add_fields(this, '#{association}', ' #{escape_javascript(fields)}')")
end
我尝试过在 fields 变量周围使用 html_safe、raw 和 f,但没有一个起作用。
使用 escape_javascript 添加的表单的 html 输出如下所示:
Start Time<input class="time" ...
使用 raw 和 html_safe 而不是 escape_javascript 会产生这样的结果:
Start Time<input class="time" ...
我想要的是这样的:
Start Time<input class="time hasDatepicker" ...
hasDatePicker 是课堂时间的 jquery 函数。这可能是我还没有意识到的简单事情。但是,感谢您提前阅读和发表评论。
如果您觉得需要更多代码,请告诉我。我想这就是所需要的一切。我正在使用 Rails 3.1 和 Ruby 1.9.2p290。
再次感谢。
I was following Railscasts 196 and 197 in order to better learn dynamic nested model forms and I can across this problem.
I am not sure how to pass javascript through the link_to_function, the class of a field contains a jquery timepicker.
def link_to_add_fields(name, f, association)
new_object = f.object.class.reflect_on_association(association).klass.new
fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
render(association.to_s.singularize + "_fields", :f => builder)
end
link_to_function(name, "add_fields(this, '#{association}', ' #{escape_javascript(fields)}')")
end
I have tried using html_safe, raw and f around the fields variable, and none have been working.
The html output of an added form using escape_javascript looks like this:
Start Time<input class="time" ...
Using raw and html_safe instead of escape_javascript produces this:
Start Time<input class="time" ...
What I would like is this:
Start Time<input class="time hasDatepicker" ...
hasDatePicker being the jquery function for the class time. It is probably something simple that I havent realized yet. But, thank you for reading and your comments ahead of time.
If you feel anymore code is required just tell me. I figure this is all that is required. I am using Rails 3.1 and Ruby 1.9.2p290.
Thanks Again.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论