Ruby on rails Ruby on Rails上不同页面上的多个更新表单

Ruby on rails Ruby on Rails上不同页面上的多个更新表单,ruby-on-rails,ruby-on-rails-5,Ruby On Rails,Ruby On Rails 5,我正在尝试将我的更新表单分离到不同的页面上。例如:一般、安全、帐户等 以下是我的路线: resources :stores, path: "stores", :as => :stores do ... match 'admin/settings' => 'stores#settings', via: :get, :as => :settings match 'admin/settings/general' => 'stores#general', via: :

我正在尝试将我的
更新
表单分离到不同的页面上。例如:一般、安全、帐户等

以下是我的路线:

resources :stores, path: "stores", :as => :stores do
  ...
  match 'admin/settings' => 'stores#settings', via: :get, :as => :settings
  match 'admin/settings/general' => 'stores#general', via: :get, :as => :settings_general
  ...
end
表单内部
常规
操作:

= form_for @store, :html => { class: "form", method: :post } do |f|
  = f.text_field :name
  ...
  = f.submit
def general
  @store = current_user.store
  if @store.update_attributes(params[:store])
    redirect_to root_path, notice: "Successfully."
  else
    redirect_to root_path, notice: "Failed."
  end
end
存储
控制器:

def general
  @store = current_user.store
  if @store.update_attributes(params[:store])
    redirect_to root_path, notice: "Successfully."
  else
    redirect_to root_path, notice: "Failed."
  end
end

private 

  def store_params
    params.require(:store).permit(:user_id, :name)
  end
出于某种原因,每次单击
submit
时,它都会将我重定向到
update
action(第页)。我找到了,但对我来说也不管用

试图将我的表单更改为:

= form_for @store, url: store_settings_general_path, :html => { class: "form", method: :post } do |f|
   ...
但它不执行任何操作。换句话说,此表单不更新值

我也试着做了一些研究,但没有发现关于这个问题的任何东西。我做错了什么?多谢各位

更新:

更新时的控制台输出:

Started POST "/stores/10/admin/settings/general" for ::1 at 2019-07-24 03:59:21 -0400
Processing by StoresController#general as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"ubak3tAazQoohB4u6EjFfFufb5HN7T6YBSdFjxhUtVEWHhTzfmUmqvdqYRpvrVolWrNrg5ZtaDyHMc+0JT2cdQ==", "store"=>{"name"=>"THIS IS A NEW NAME"}, "commit"=>"Update Store", "store_id"=>"10"}
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ?  [["id", 2], ["LIMIT", 1]]
  Store Load (0.1ms)  SELECT  "stores".* FROM "stores" WHERE "stores"."user_id" = ? LIMIT ?  [["user_id", 2], ["LIMIT", 1]]
  Rendering stores/general.html.haml within layouts/seller
  Rendered stores/general.html.haml within layouts/seller (4.6ms)
  Rendered global/_navbar.html.haml (11.5ms)
  Rendered global/_stores-sidebar.html.haml (5.5ms)
Filter chain halted as :stores_admin_layout rendered or redirected
Completed 200 OK in 138ms (Views: 135.2ms | ActiveRecord: 0.3ms)
请注意,
名称
值是一个新值,但是它不会更新
存储
表中的值

概述中的表格

= form_for @store, url: store_settings_general_path, :html => { class: "form", method: :post } do |f|

从评论来看,你似乎已经弄清了事情的真相。FWIW,还有一些其他的注释

您的路线声明似乎有点过时。我想我会:

resources :stores do
  member do
    patch 'admin/settings',         action: :settings,  as: :settings
    patch 'admin/settings/general', action: :general,   as: :settings_general
  end
end 
在国际海事组织看来,这似乎更加现代,并将给你:

         settings_store PATCH  /stores/:id/admin/settings(.:format)               stores#settings
 settings_general_store PATCH  /stores/:id/admin/settings/general(.:format)       stores#general
                 stores GET    /stores(.:format)                                  stores#index
                        POST   /stores(.:format)                                  stores#create
              new_store GET    /stores/new(.:format)                              stores#new
             edit_store GET    /stores/:id/edit(.:format)                         stores#edit
                  store GET    /stores/:id(.:format)                              stores#show
                        PATCH  /stores/:id(.:format)                              stores#update
                        PUT    /stores/:id(.:format)                              stores#update
                        DELETE /stores/:id(.:format)                              stores#destroy
你可能想好好读一读。鉴于此,在您的
常规
操作中,您正在更新
@store
,我使用了补丁

我还想看看你是如何使用的。我相信您可能不需要使用
:html=>{}
,这里:

= form_for @store, :html => { class: "form", method: :post } do |f|
您可能可以执行以下操作:

= form_for settings_general_store_path(@store), class: 'form' do |f|
我不记得Rails是否足够聪明,可以在您提供
@store
时使用“补丁”提交,因此您可能需要执行以下操作:

= form_for settings_general_store_path(@store), class: 'form', method: :patch do |f|    
但是,试一试,看看会发生什么

在您的
常规
操作中:

= form_for @store, :html => { class: "form", method: :post } do |f|
  = f.text_field :name
  ...
  = f.submit
def general
  @store = current_user.store
  if @store.update_attributes(params[:store])
    redirect_to root_path, notice: "Successfully."
  else
    redirect_to root_path, notice: "Failed."
  end
end
我不知道在定义
store\u params
时,为什么要使用
params[:store]
。所以,我会做一些更像:

def general
  @store = current_user.store
  if @store.update_attributes(store_params)
    redirect_to root_path, notice: "Successfully."
  else
    redirect_to root_path, notice: "Failed."
  end
end

我想你的路线中没有通过邮局的路线。rb@evgeniy_trebin我试图通过:[:get,:post]
添加
,但仍然不起作用。此外,我还尝试在表单中添加相同的方法。。谢谢,
form\u为@store
生成的URL是什么?@jvillian
update\u store\u path
。如果我使用表单的第一个变体,我会收到一个错误:
缺少模板存储/更新…
,因为我没有
更新
页面。这是什么:
过滤器链暂停为:存储\管理\呈现或重定向布局