Rails路由错误与form_tag

时间:2023-01-23 20:34:42

Firstly, I am new to Rails so please excuse me if this is a lame newbie question

I am creating specials (which belongs_to :shop) with a rails form_tag

In my admin/specials/new.html.erb, the form looks like this:

首先,我是Rails的新手,所以请原谅我,如果这是一个蹩脚的新手问题,我正在创建特殊版(属于:shop),带有rails form_tag在我的admin / specials / new.html.erb中,表单如下所示:

<%= form_tag new_admin_shop_special_path(@current_shop) do |f| %>
  <input type = "number" name = "product_ids" />

<% end %>

(The path new_admin_shop_special_path definitely does exist)
In rake routes I have:

(路径new_admin_shop_special_path肯定存在)在rake路由中我有:

                       POST   /admin/shops/:shop_id/specials(.:format)                            admin/specials#create
new_admin_shop_special GET    /admin/shops/:shop_id/specials/new(.:format)                           admin/specials#new


My admin/specials_controller.rb has:

我的admin / specials_controller.rb有:

  def new
  end

  def create
    special = @current_shop.specials.build
    special.add_products(params[:product_ids])
    redirect_to admin_shop_shipping_options_path, notice: "#{special.id}"
  end

(The notice is for debugging)

So, problem is, if I submit the form, I get an error saying

(通知是为了调试)所以,问题是,如果我提交表单,我会收到一个错误说

Routing Error

No route matches [POST] "/admin/shops/dear-rae/specials/new"

没有路线匹配[POST]“/ admin / shops / dear-rae / specials / new”


Help.

帮帮我。

1 个解决方案

#1


0  

This should work, assuming you use resources:

假设您使用资源,这应该有效:

<%= form_tag admin_shop_specials_path(@current_shop) do |f| %>

And yes, the path with new on the end does exist, but only with GET method. When you submit your form, POST method is used, which is not applicable for this route.

是的,结尾的新路径确实存在,但只有GET方法。提交表单时,使用POST方法,该方法不适用于此路由。

#1


0  

This should work, assuming you use resources:

假设您使用资源,这应该有效:

<%= form_tag admin_shop_specials_path(@current_shop) do |f| %>

And yes, the path with new on the end does exist, but only with GET method. When you submit your form, POST method is used, which is not applicable for this route.

是的,结尾的新路径确实存在,但只有GET方法。提交表单时,使用POST方法,该方法不适用于此路由。