Rails:页面重新加载时删除数据
我的应用程序助手中有一个方法,旨在停用数据库中的任务。
应用程序助手:
def link_to_destroy_task(task_id)
Task.find(task_id).deactivate
end
模型:
def self.deactivate()
update_attribute(:active, false)
end
视图:
<ol id="tasks">
<% @Tasks.each do |task| %>
<%content_tag_for :li, task do %>
<span class ="handle"> [drag] </span>
<%= link_to task.name, :controller => "com_tasks", :action => "edit", :id => task.id %>
<%= link_to_function '[Delete]', link_to_destroy_task(task.id) %>
<% end %>
<% end %>
</ol>
由于某种原因,当页面加载时,它会删除列表中的所有内容。不仅仅是将 active 切换为 false。它正在将其从数据库中完全删除。
我使用的是fixture,所以我重新加载了所有测试数据,我可以刷新一次再次看到数据,但是下次重新加载时它就消失了。
我已将该方法的名称更改为“停用”,因为我认为 destroy 已被执行,但该方法仍在运行而没有被单击。它不再删除,而是停用所有内容。
I have a method in my application helper that is meant to deactivate a task in my database.
Application helper:
def link_to_destroy_task(task_id)
Task.find(task_id).deactivate
end
Model:
def self.deactivate()
update_attribute(:active, false)
end
View:
<ol id="tasks">
<% @Tasks.each do |task| %>
<%content_tag_for :li, task do %>
<span class ="handle"> [drag] </span>
<%= link_to task.name, :controller => "com_tasks", :action => "edit", :id => task.id %>
<%= link_to_function '[Delete]', link_to_destroy_task(task.id) %>
<% end %>
<% end %>
</ol>
For some reason when the page loads it is deleting everything from the list. Not just switching active to false. It is completely wiping it from the database.
I am using fixtures, so I reload all the test data, and I can refresh once to see the data again, but the next time I reload it is gone.
I have changed the name of the method to 'deactivate' because I think destroy is already taken, but the method is still being run without being clicked. It isn't deleting anymore, but it is deactivating everything.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
发生这种情况是因为您在视图中为每个项目调用
deactivate
! link_to_function 的第二个参数正在运行实际的停用操作。我认为您误解了
link_to_function
- 单击时它不会运行 ruby 函数,而是运行您创建的 javascript 函数。第二个参数必须是包含 JavaScript 函数的字符串。 Rails 无法在某人的浏览器中运行 ruby 方法。以下是文档的链接,它应该可以帮助您走上正确的道路:
http://apidock .com/rails/ActionView/Helpers/JavaScriptHelper/link_to_function
This is happening because you're calling
deactivate
in your view for each item! The 2nd parameter to your link_to_function is running the actual deactivation.I think you're misunderstanding
link_to_function
- it doesn't run a ruby function when clicked, it runs a javascript function you've created. The 2nd parameter needs to be a string that contains a javascript function. Rails would have no way to run a ruby method in somebody's browser.Here's a link to the documentation, which should get you on the right track:
http://apidock.com/rails/ActionView/Helpers/JavaScriptHelper/link_to_function
请注意这一行,
您实际上是在此处执行
link_to_destroy_task
函数,每个任务执行一次。来自文档
def link_to_function(name, *args, &block)
返回给定名称+的链接,该链接将使用 onclick 处理程序触发 JavaScript +函数+,并在事后返回 false。
从未使用过此方法,但似乎其目的是调用 JavaScript 函数,而不是Ruby 函数。
也有例子。
Pay attention to this line
You're actually executing
link_to_destroy_task
function here, once for each task.From the docs
def link_to_function(name, *args, &block)
Returns a link of the given +name+ that will trigger a JavaScript +function+ using the onclick handler and return false after the fact.
Never used this method, but it seems its purpose is to invoke JavaScript function, not Ruby function.
There're examples too.