在没有 ID 的情况下调用新方法的路由问题
我正在尝试将编辑多个 Shift 对象的 form_tag 放在一起。我已经正确构建了表单,并且它传递了正确的参数。我已经验证这些参数可以在控制台中正确更新对象。但是,当我单击“提交”按钮时,出现错误:
ActiveRecord::RecordNotFound in ShiftsController#update_individual
Couldn't find Shift without an ID
我正在调用的控制器的路由看起来像这样:
map.resources :shifts, :collection => { :update_individual => :put }
ShiftsController 中的方法是这样的:
def update_individual
Shift.update(params[:shifts].keys, params[:shifts].values)
flash[:notice] = "Schedule saved"
end
相关的表单部分是这些:
<% form_tag( update_individual_shifts_path ) do %>
... (fields for...)
<%= submit_tag "Save" %>
<% end %>
为什么这不起作用?如果我浏览到网址:“http://localhost:3000/shifts/update_individual/5”(或与现有班次相对应的任何数字),我收到有关未设置参数的正确错误,但是当我传递没有某种 ID 的参数时,它会出错。
如何让它停止在 URL 末尾查找 ID?
I'm trying to put together a form_tag that edits several Shift objects. I have the form built properly, and it's passing on the correct parameters. I have verified that the parameters work with updating the objects correctly in the console. However, when I click the submit button, I get the error:
ActiveRecord::RecordNotFound in ShiftsController#update_individual
Couldn't find Shift without an ID
My route for the controller it is calling looks like this looks like this:
map.resources :shifts, :collection => { :update_individual => :put }
The method in ShiftsController is this:
def update_individual
Shift.update(params[:shifts].keys, params[:shifts].values)
flash[:notice] = "Schedule saved"
end
The relevant form parts are these:
<% form_tag( update_individual_shifts_path ) do %>
... (fields for...)
<%= submit_tag "Save" %>
<% end %>
Why is this not working? If I browse to the url: "http://localhost:3000/shifts/update_individual/5" (or any number that corresponds to an existing shift), I get the proper error about having no parameters set, but when I pass parameters without an ID of some sort, it errors out.
How do I make it stop looking for an ID at the end of the URL?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你需要告诉表单标签助手你想使用 PUT 而不是 POST
I think that you need to tell the form tag helper you want to use PUT instead of POST
令人惊讶的是,事实证明我能够通过重命名方法和传递虚拟变量的组合来解决这个问题。更改是对以下行:
form.html.erb:
paths.rb:
在那里,我每次都会向它传递一个 ID 4,没关系,它实际上并没有对它去抓取的 shift 对象做任何事情,它只是 。 ..我不知道,我猜是黑客攻击。
shifts_controller.rb:
Amazingly, it turns out that I was able to fix this by a combination of renaming the method and passing a dummy variable. Changes were to the lines:
form.html.erb:
routes.rb:
There I pass it an ID of 4 every time, it doesn't matter, it's not actually doing anything with the shift object it goes and grabs, it's just ... I don't know, a hack, I guess.
shifts_controller.rb: