【发布时间】: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