ruby-on-rails - Controller Action 被调用两次

标签 ruby-on-rails controller action

关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。












想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。

上个月关闭。




Improve this question




我注意到,我的 Controller 的“索引”操作被调用了两次。

该 Action 具有以下结构:

def index

  if params[:tags].nil?
     # [fork #1] just return the whole collection of model
     @items = Item.all
  else
     # [fork #2] filter items by tags
     @items = Item.select_by_tags params[:tags]
  end

  # Another operations with @items
  # The result will be incorrect, because, when tags in params are specified,
  # controller action will be first executed for fork #2, and then for fork #1.
  # In view, i get @items from fork #2 and result of THIS piece of code for fork #1
end

在页面上,我有 URL 链接,比如“/items?tags=tag1, tag2”,点击它们我得到“index” Action ,调用两次

我不知道,为什么会发生这种情况......

最佳答案

我已经想通了。

这种行为的原因是 JavaScript 代码,在页面加载后被调用。此代码为页面的某些部分实现了深层链接。

小心这一点。

谢谢。

关于ruby-on-rails - Controller Action 被调用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3930332/

相关文章:

jsf - 在页面加载时调用 JSF 托管 Bean 操作

button - 在 Shiny 的情况下,我如何使用操作按钮的值更新输入文本字段

java - 通过热键执行相应操作时动画按钮

ruby-on-rails - 无 rspec 期望的 capybara have_field 在升级到 2.2 时失败

javascript - 更新 Rails Foundation-rails gem 5 到 6,datepicker 不起作用,页面加载时出现 JS 错误

ruby-on-rails - ruby rails : Deleting multiple attachments in paperclip on updates

ruby-on-rails - 让 imgkit + 回形针 + s3 工作。回形针无法识别 "snapshot"

php - Symfony - 为命名路由生成 URL 但不存在

ruby-on-rails - 类型错误 : can't convert Post into Array

ruby-on-rails - 在 Rails 控制台中调用 Controller 方法时,如何将参数传递给 Controller ​​方法?