Ruby on rails 将提交按钮路由到自定义路径

Ruby on rails 将提交按钮路由到自定义路径,ruby-on-rails,Ruby On Rails,我试图将提交按钮路由到特定路径(页面),但我认为我的语法不准确 这就是我现在拥有的: <%= submit_tag('Next (Step 2 of 3)'), customer_index_path %> 我也试过这个: <%= submit_tag'Next (Step 2 of 3)', customer_index_path %> 如何将提交路由到特定路径?路由的路径必须包含在参数列表中,因此在代码的第一次迭代中,请确保两个参数都包含在括号中: <%=

我试图将提交按钮路由到特定路径(页面),但我认为我的语法不准确

这就是我现在拥有的:

<%= submit_tag('Next (Step 2 of 3)'), customer_index_path %>
我也试过这个:

<%= submit_tag'Next (Step 2 of 3)', customer_index_path %> 

如何将提交路由到特定路径?

路由的路径必须包含在参数列表中,因此在代码的第一次迭代中,请确保两个参数都包含在括号中:

<%= submit_tag('Next (Step 2 of 3)', options) %>
更新

关于传递给
submit\u tag
的第二个参数,请说出以下内容:

 submit_tag(value = "Save changes", options = {}) 
以下是有效的选项:

  • :data-此选项可用于添加自定义数据属性
  • :disabled-如果为true,用户将无法使用此输入
  • 任何其他键都会为标记创建标准HTML选项
请注意路径不是有效值。相反,应该将路径作为参数传递给opening
form\u标记
helper

此外,我假设–因为您没有为使用
表单_–您没有此控制器的资源路径。因此,您需要为
customer\u index\u path
创建自定义路由:

# config/routes.rb
get '/customers', to: 'customers#index', :as 'customers_index'

您不在中包含路径。您需要在
表单
操作
中定义路径

<%= form_tag(customer_index_path) do %>
  <%= submit_tag 'Next (Step 2 of 3)' %> 
<% end %>

我仍然收到错误:未定义“/customer/index”的方法“stringify\u keys”:StringSee my update。您不能将路径作为参数传递给
submit\u-tag
–您应该将路径传递给打开的
表单\u-tag
。我在
submit\u tag
中为第二个
options
参数包含了一个有效值列表。这更有意义,但现在我遇到了这个错误:没有与[POST]“/customer/index”匹配的路由。我以前遇到过这个错误,对此我有点理解。之所以会发生这种情况,是因为路由是get http动词,而不是post。尽管如此,我不确定解决这个问题的正确方法。我试着在我的路线上贴上“客户/索引”,但似乎没有right@anmaree,正确,您将得到
无路由匹配[POST]…
,因为默认情况下表单的方法是
POST
。如果需要使用
GET
,则需要将
method::GET
添加到
表单标签中。
 submit_tag(value = "Save changes", options = {}) 
# config/routes.rb
get '/customers', to: 'customers#index', :as 'customers_index'
<%= form_tag(customer_index_path) do %>
  <%= submit_tag 'Next (Step 2 of 3)' %> 
<% end %>
<%= form_tag(customer_index_path, method: :get) do %>
  <%= submit_tag 'Next (Step 2 of 3)' %> 
<% end %>