Rails 自定义操作缺少必需的键错误
Rails custom action missing required key error
show.html.erb
<h1><%= @script.Name %></h1>
<%= simple_form_for @script , :url => script_execute_path do |f| %>
<%= f.input :mainDirectory %>
<%= f.input :customFilePath %>
<%= f.button :submit, 'Run' %>
<% end %>
routes.rb
root :to => "scripts#index"
resources :scripts do
get "execute"
end
型号
class AddNameToScript < ActiveRecord::Migration
def change
add_column :scripts, :Name, :string
add_column :scripts, :Description, :text
end
end
execute
是我添加的自定义操作,我想从 show
.
转到该操作
routes.rb
...
script_execute GET /scripts/:script_id/execute(.:format) scripts#execute
...
但是我收到一个错误
No route matches {:action=>"execute", :controller=>"scripts", :id=>"1"}
missing required keys: [:script_id]
但是为什么我需要 [:script_id]
?这不是自定义操作,我可以定义我想要的方式吗?这里缺少什么以及如何传递 [:script_id]
?
将 :id
传递给您的表单助手并尝试
<%= simple_form_for @script , :url => script_execute_path(@script) do |f| %>
<%= f.input :mainDirectory %>
<%= f.input :customFilePath %>
<%= f.button :submit, 'Run' %>
<% end %>
正如您从路由器输出中看到的那样,execute
是 需要一个 script_id
参数 (/scripts/:script_id/execute(.:format
)。要消除这种期望,您需要在 collection
而不是 member
级别声明该路由。您可以通过以下两种方式之一执行此操作:
get :execute, on: :collection
或者如果您有其他路线想要包含在集合中,您可以将其放入一个块中:
collection do
get :execute
end
仅供参考,在此更改后 运行 rake routes | grep execute
再次查看 URL 助手的名称已更改为什么,以便您可以相应地更新您的视图。
干杯!
更新:
要回答评论中的问题,如果您想传入一个 script_id
参数(或与此相关的任何参数),您可以通过将其声明为 [=51 的参数来实现=] 帮手。所以根据你目前的看法,它看起来像这样:
<h1><%= @script.Name %></h1>
<%= simple_form_for @script , :url => script_execute_path(script_id: @script.id) do |f| %>
<%= f.input :mainDirectory %>
<%= f.input :customFilePath %>
<%= f.button :submit, 'Run' %>
<% end %>
但是,我想在这里再指出几件事:
- 您似乎打算更新
Script
对象,所以我不确定您为什么要从 show
视图创建 execute
方法,而不是仅使用 edit
视图中已通过 resources :scripts
提供的 update
方法。
- 同样,根据您的观点,您似乎打算 PUT 或 POST 某些内容,但您的
execute
路由被定义为 GET。不确定这是否是您想要的。
show.html.erb
<h1><%= @script.Name %></h1>
<%= simple_form_for @script , :url => script_execute_path do |f| %>
<%= f.input :mainDirectory %>
<%= f.input :customFilePath %>
<%= f.button :submit, 'Run' %>
<% end %>
routes.rb
root :to => "scripts#index"
resources :scripts do
get "execute"
end
型号
class AddNameToScript < ActiveRecord::Migration
def change
add_column :scripts, :Name, :string
add_column :scripts, :Description, :text
end
end
execute
是我添加的自定义操作,我想从 show
.
routes.rb
...
script_execute GET /scripts/:script_id/execute(.:format) scripts#execute
...
但是我收到一个错误
No route matches {:action=>"execute", :controller=>"scripts", :id=>"1"}
missing required keys: [:script_id]
但是为什么我需要 [:script_id]
?这不是自定义操作,我可以定义我想要的方式吗?这里缺少什么以及如何传递 [:script_id]
?
将 :id
传递给您的表单助手并尝试
<%= simple_form_for @script , :url => script_execute_path(@script) do |f| %>
<%= f.input :mainDirectory %>
<%= f.input :customFilePath %>
<%= f.button :submit, 'Run' %>
<% end %>
正如您从路由器输出中看到的那样,execute
是 需要一个 script_id
参数 (/scripts/:script_id/execute(.:format
)。要消除这种期望,您需要在 collection
而不是 member
级别声明该路由。您可以通过以下两种方式之一执行此操作:
get :execute, on: :collection
或者如果您有其他路线想要包含在集合中,您可以将其放入一个块中:
collection do
get :execute
end
仅供参考,在此更改后 运行 rake routes | grep execute
再次查看 URL 助手的名称已更改为什么,以便您可以相应地更新您的视图。
干杯!
更新:
要回答评论中的问题,如果您想传入一个 script_id
参数(或与此相关的任何参数),您可以通过将其声明为 [=51 的参数来实现=] 帮手。所以根据你目前的看法,它看起来像这样:
<h1><%= @script.Name %></h1>
<%= simple_form_for @script , :url => script_execute_path(script_id: @script.id) do |f| %>
<%= f.input :mainDirectory %>
<%= f.input :customFilePath %>
<%= f.button :submit, 'Run' %>
<% end %>
但是,我想在这里再指出几件事:
- 您似乎打算更新
Script
对象,所以我不确定您为什么要从show
视图创建execute
方法,而不是仅使用edit
视图中已通过resources :scripts
提供的update
方法。 - 同样,根据您的观点,您似乎打算 PUT 或 POST 某些内容,但您的
execute
路由被定义为 GET。不确定这是否是您想要的。