如何在下一个示例中创建ajax:
在 Controller 中:
def index
if params[:p] == "one"
@record = "Hello, 1!"
elsif params[:p] == "two"
@record = "Hello, 2!"
else
@record = "something else"
end
end
在 View 中(hellos Controller ,索引操作):
<%= link_to "One", hellos_path(:p => "one") %>
<%= link_to "Two", hellos_path(:p => "two") %>
<%= render :partial => 'record' %>
部分_record.html.erb:
<%= @record %>
仅此而已。索引 Controller 中的所有操作和仅参数正在更改。我不想重新加载整个页面 - 仅使用新的 Controller 变量记录部分内容。
如何“AJAX化”它? :)
最佳答案
1- 将 :remote => true 添加到您的链接:
<%= link_to "One", hellos_path(:p => "one"), :remote => true %>
2- 创建一个 View index.js.erb:
$("#your_div").html("<%= render :partial => "record" %>")
3- 在您的index.html.erb 中创建一个ID 为your_div 的div。该 div 将使用 @record 对象填充。
关于ruby-on-rails - Rails 3.2 ajax 示例, Controller 中带有参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12818555/