在没有 ID 的情况下调用新方法的路由问题

发布于 2024-08-28 00:38:01 字数 978 浏览 5 评论 0原文

我正在尝试将编辑多个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

稚气少女 2024-09-04 00:38:01

我认为你需要告诉表单标签助手你想使用 PUT 而不是 POST

<% form_tag( update_individual_shifts_path, :method => :put) do %>
  ... fields ....
  <%= submit_tag "Save" %>
<% end %>

I think that you need to tell the form tag helper you want to use PUT instead of POST

<% form_tag( update_individual_shifts_path, :method => :put) do %>
  ... fields ....
  <%= submit_tag "Save" %>
<% end %>
提笔书几行 2024-09-04 00:38:01

令人惊讶的是,事实证明我能够通过重命名方法和传递虚拟变量的组合来解决这个问题。更改是对以下行:

form.html.erb:

<% form_tag( poop_individual_shifts_path ) do %>

paths.rb:

map.poop_individual_shifts "poop_shifts", :controller => 'shifts', :action => "poop_individual", :method => "put", :id => 4
map.resources :shifts 

在那里,我每次都会向它传递一个 ID 4,没关系,它实际上并没有对它去抓取的 shift 对象做任何事情,它只是 。 ..我不知道,我猜是黑客攻击。

shifts_controller.rb:

def poop_individual

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:

<% form_tag( poop_individual_shifts_path ) do %>

routes.rb:

map.poop_individual_shifts "poop_shifts", :controller => 'shifts', :action => "poop_individual", :method => "put", :id => 4
map.resources :shifts 

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:

def poop_individual
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文