如何更新“div” ActiveRecord 值更改后的内容无需重新加载页面?

发布于 2024-10-11 21:45:47 字数 818 浏览 3 评论 0原文

我正在将 Ruby on Rails 3 与 Prototype/Scriptaculous 结合使用,并且尝试在更改 ActiveRecord 值后更新“div”内容,而无需重新加载页面。

在“edit.html.erb”中,我有:

...

<div id="test_id">
  <%= @account.name.to_s %>
</div>

<%= link_to_function( "Make test" ) do |page|
  page.replace_html :test_id, @account.name.to_s
end %>

...

在单击“Make test”之前,我更新了“@account.name”值,甚至通过 AJAX 也是如此。然后,单击“进行测试”,模板不会更改。

这些是步骤:

  1. 页面
  2. 我显示通过 AJAX 更新“@account.name”的
  3. 我点击“Make test”
  4. “div”,其中“id=”test_id”不会改变!

我做错了什么?

PS:我认为@account 不会在页面中重新加载,即使他的值在数据库中发生更改也是如此。如果是这样,我该怎么办?


我看过Railscasts“AJAX with RJS”,我遵循了其中的一个例子(就像我的),但它对我不起作用。

I am using Ruby on Rails 3 with Prototype/Scriptaculous and I am trying to update a 'div' content after that ActiveRecord values are changed, without reload the page.

In the "edit.html.erb" I have:

...

<div id="test_id">
  <%= @account.name.to_s %>
</div>

<%= link_to_function( "Make test" ) do |page|
  page.replace_html :test_id, @account.name.to_s
end %>

...

Before clicking on "Make test" I update the '@account.name' value, even via AJAX. Then, clicking on "Make test", the template doesn't changes.

These are steps:

  1. I show the page
  2. I update '@account.name' via AJAX
  3. I click on "Make test"
  4. 'div' with 'id="test_id"' doesn't change!

What am I doing wrong?

P.S.: I think that @account is not reloaded in the page, also if his values are changed in the database. If so, what should I do?


I have seen the Railscasts "AJAX with RJS" and I followed an example in that (it is like the my), but it doesn't work for me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

む无字情书 2024-10-18 21:45:47

如果您在 @account.name 的值为“George”时渲染了 edit.html.erb,则 HTML 将保持不变(值为“George”),直到您刷新它。您对 link_to_function 的调用是在页面加载时呈现一个 html 链接,该链接调用 javascript,将“test_id”div 的内部 html 替换为“George”。除非您替换该 HTML,否则它将始终将该 div 的内部 html 替换为“George”。

在不确切知道您想要做什么的情况下,很难推荐解决方案...

更新以获取更多详细信息:

如果您正在进行 AJAX 调用来更改服务器上帐户名称的值为“Fred”,并且希望该更改出现在页面上,您应该刷新页面中在同一 AJAX 调用(同一控制器操作)中使用该值的部分。

您的 link_to_function 生成如下 HTML(如果渲染页面时 @account.name 为“George”):

<a onclick="try { Element.update("test_id", "George"); ... >Make test</a>

它不是 ajax 调用,它只是一个执行 javascript 的链接。如果您想让它成为一个 Ajax 调用来查找帐户名称的最新值并刷新 test_id div,请执行以下操作:

<%# you need prototype included, but it should probably be in application.rhtml %> 
<%= javascript_include_tag "prototype.js" %>

<div id="test_id">
  <%= @account.name.to_s %>
</div>

<%= link_to_remote "Make test", :url => { :controller => "account", :action => "change_name" } %>

“Make test”链接现在将执行对“account”控制器中的“change_name”方法。这个方法看起来像这样:

def change_name
  # here you would load the account from the db; I'm cheating
  @account = Account.new(:name => "Fred")

  render :update do |page|
    page.replace_html :test_id, :text => @account.name.to_s
  end
end

If you have rendered edit.html.erb when the value of @account.name is "George", the HTML will remain the same (with the value "George") until you refresh it. Your call to link_to_function is, on page load, rendering an html link that calls javascript that replaces the inner html of the "test_id" div with 'George'. Unless you replace that HTML, it will always replace the inner html of that div with 'George'.

It's hard to recommend a solution without knowing exactly what you'd like to do...

updated to have more detail:

If you are making an AJAX call that changes the value of the account name on the server to "Fred", and want that change to appear on the page, you should refresh the parts of the page that use that value in that same AJAX call (that same controller action).

Your link_to_function generates HTML like this (if @account.name was 'George' when the page was rendered):

<a onclick="try { Element.update("test_id", "George"); ... >Make test</a>

It is not an ajax call, it is just a link that executes javascript. If you want to make it an ajax call that finds the latest value of the account name and refreshes the test_id div, do something like this:

<%# you need prototype included, but it should probably be in application.rhtml %> 
<%= javascript_include_tag "prototype.js" %>

<div id="test_id">
  <%= @account.name.to_s %>
</div>

<%= link_to_remote "Make test", :url => { :controller => "account", :action => "change_name" } %>

The 'Make test' link will now perform an AJAX call to the 'change_name' method in the 'account' controller. This method would look something like this:

def change_name
  # here you would load the account from the db; I'm cheating
  @account = Account.new(:name => "Fred")

  render :update do |page|
    page.replace_html :test_id, :text => @account.name.to_s
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文