繁体   English   中英

Rails 中没有路线匹配 [POST]

[英]No route matches [POST] in Rails

在提交 forms 时出现路由错误

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 开始 POST "/sellers/1/seller_profiles/new" for::1

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 你的意思是?seller_path):

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

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

这可确保输入将包含验证失败时用户输入的值。 它还连接到 I18n API 进行翻译,以便使用 model 的翻译键而不是通用键。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM