![](/img/trans.png)
[英]how to pass variable from view in controller in javascript - Rails 4
[英]How to pass variable from javascript to view in rails?
当我单击提交按钮时,我在 controller 中使用 json 进行渲染。
我的 json 是{notes: array[1], array[2]}
。 现在我正在渲染view.html.erb file
。 我想在这个文件中显示像<%= notes %>
这样的笔记中的值,如何显示呢?
如果notes
是一个数组,您可以遍历它并在.erb.html
文件中显示内容,如下所示:
<div> <% notes.each_with_index do | note, index | %> <div id="title"> <%= note[:title] %> </div> <% end %> </div>
听起来您将 JSON 请求与 HTML 请求混淆了。
传入的请求应该是 HTML
controller
# GET /the_controller/view
# not /the_controller/view.json <- wrong
def view
@notes = array
end
查看.html.erb
<%= @notes %>
请参阅@rishabh0211 答案以获得更好的演示文稿
如果您确实打算将其作为 json 请求,通过 som ajax 请求或类似请求,您需要在您的问题中指定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.