【问题标题】:Ruby Rails: Controller not passing ID correctly?Ruby Rails:控制器未正确传递 ID?
【发布时间】:2012-11-07 13:09:06
【问题描述】:

我有一个链接可以切换数据库中属性的真/假。对于两个不同的属性,我有两个版本的此链接,一个有效,另一个无效,除非我强制使用特定 ID,否则它可以正常工作。

工作链接视图:

<h1><%= link_to "Toggle True", toggle_completed_true_task_path(@task), :remote => true %></h1>

<h1><%= link_to "Toggle False", toggle_completed_false_task_path(@task), :remote => true %></h1>

工作控制器视图:

respond_to :html, :js
  def toggle_completed_true
    @task = Task.find(params[:id])
    @task.update_attributes(:completed => true)
  end

  respond_to :html, :js
  def toggle_completed_false
    @task = Task.find(params[:id])
    @task.update_attributes(:completed => false)
  end

失败链接的视图:

<h1><%= link_to "Toggle True", toggle_confirmed_true_task_path(@task), :remote => true %></h1>

<h1><%= link_to "Toggle False", toggle_confirmed_false_task_path(@task), :remote => true %></h1>

失败控制器的视图:

respond_to :html, :js
  def toggle_confirmed_true
    @task = Task.find(params[:id])
    @task.update_attributes(:confirmed => true)
  end

  respond_to :html, :js
  def toggle_confirmed_false
    @task = Task.find(params[:id])
    @task.update_attributes(:confirmed => false)
  end

我已经为此工作了几个小时,在我的一生中,我无法理解为什么一个会失败而另一个会工作。 注意这两个出现在同一页面上,如果我像这样传递一个特定的 ID,那一个不起作用的将起作用:

<h1><%= link_to "Toggle True", toggle_confirmed_true_task_path(12), :remote => true %></h1>

我使用变量 pass 得到的具体错误是: "没有路线匹配 {:action=>"toggle_confirmed_true", :controller=>"tasks", :id=>nil"

非常感谢任何见解。

【问题讨论】:

  • 你记得把它添加到你的 routes.rb 文件吗?
  • 我的回答对@Nathan 有帮助吗?

标签: ruby-on-rails ruby hyperlink controller


【解决方案1】:

运行“rake routes”命令。这将按照它们在 routes.rb 中出现的顺序打印您的所有路线。确认订单。

您要查找的路径应按顺序排在第一位。

【讨论】:

  • 我已经做到了,以下是工作和不工作的路线toggle_confirmed_true_task GET /tasks/:id/toggle_confirmed_true(.:format) tasks#toggle_confirmed_true toggle_confirmed_false_task GET /tasks/:id/toggle_confirmed_false(.:format) tasks#toggle_confirmed_false toggle_completed_true_task GET /tasks/:id/toggle_completed_true(.:format) tasks#toggle_completed_true toggle_completed_false_task GET /tasks/:id/toggle_completed_false(.:format) tasks#toggle_completed_false
  • 错误提示“id”为 nil,请验证 @task 对象,它不应为 null 或空。我使用变量 pass 得到的具体错误是:"No route matches {:action=>"toggle_confirmed_true", :controller=>"tasks", :id=>nil"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-29
  • 2016-08-06
  • 1970-01-01
  • 2016-11-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多