【问题标题】:No route matches [POST] in RailsRails 中没有路线匹配 [POST]
【发布时间】:2021-01-11 21:44:46
【问题描述】:

提交表单时出现路由错误

resources :sellers do
    resources :seller_profiles
end
<%= form_for seller_seller_profiles_path do |form| %>
  <div class="field">
    <%= form.label :first_name %>
    <%= form.text_field :first_name %>
  </div>

  <div class="field">
    <%= form.label :other_name %>
    <%= form.text_field :other_name %>
  </div>

   -- more similar fields ---

  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>
 def create 
    @seller_profile = @seller.seller_profile.create!(seller_profile_params)

    respond_to do |format|
      format.html { redirect_to root_path}
    end

错误信息: 于 2021-01-11 21:37:01 +0000 开始为 ::1 发布“/sellers/1/seller_profiles/new”

ActionController::RoutingError(没有路由匹配 [POST] "/sellers/1/seller_profiles/new"):

更新

我把视图改为

<%= form_with(model: [@seller, @seller_profile], local: true) do |form| %>
--- code continues ---

现在新路由可以工作,但编辑路由会抛出此错误 ActionView::Template::Error (undefined method `seller_profile_path' for #ActionView::Base:0x0000000001d060 你的意思是?卖家路径):

【问题讨论】:

  • 该新错误可能与该视图附带的按钮之一有关,您可能需要更新路径。

标签: ruby-on-rails


【解决方案1】:

改为传递模型实例,以便您的输入绑定到模型实例。对于嵌套资源,传递一个数组:

<%= form_for([@seller, @seller_profile]) do |form| %>

这可确保输入将包含验证失败时用户输入的值。它还挂钩到 I18n API 进行翻译,因此它将使用该模型的翻译键而不是通用键。

如果您使用的是 Rails 5.1+,请改用 form_with(model: [@seller, @seller_prodile], local: true)

【讨论】:

  • 它现在可以工作,但不幸的是在编辑路线上我收到此错误 ActionView::Template::Error (undefined method `seller_profile_path' for #<:base:0x0000000001c610> 你的意思是?seller_path ): 1:
  • 这确实是一个后续问题 - 你问了一个单独的问题很好。您应该考虑回滚对问题的编辑,因为作为编辑提出的后续问题在 stackoverflow 上不受欢迎,因为它们会导致范围不断扩大。 meta.stackoverflow.com/questions/266767/…
猜你喜欢
  • 2013-12-31
  • 1970-01-01
  • 2021-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多