我正在尝试在不同的页面上分离我的update表单。例如:一般、安全、帐户等。
以下是我的路线:
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表单在general操作中:
= form_for @store, :html => { class: "form", method: :post } do |f|
= f.text_field :name
...
= f.submitStores控制器:
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操作(页面)。我找到了这个答案,但对我也没用。
试图将我的表单更改为:
= form_for @store, url: store_settings_general_path, :html => { class: "form", method: :post } do |f|
...但它不能表演任何动作。换句话说,此表单不更新值。
我也试图做一些研究,但没有发现任何关于这个问题。我做错了什么?非常感谢。
更新:
update上的控制台输出
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)请注意,name值是一个新值,但是它不会更新stores表中的值。
表格在general中
= form_for @store, url: store_settings_general_path, :html => { class: "form", method: :post } do |f|发布于 2019-07-24 14:16:55
从评论中看,你已经了解了事情的真相。还有几个音符..。
您的路线声明似乎有点过时。我想我会:
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您可能需要在放置vs贴片上阅读。考虑到在您的general操作中,您正在更新@store,我使用了修补程序。
我还想看看您是如何使用为的。我相信您可能不需要使用:html => {},在这里:
= form_for @store, :html => { class: "form", method: :post } do |f|你也许能做到:
= form_for settings_general_store_path(@store), class: 'form' do |f|当您提供@store时,我不记得Rails是否聪明到能够提交“修补程序”,所以您可能需要这样做:
= form_for settings_general_store_path(@store), class: 'form', method: :patch do |f| 但是,试试看会发生什么。
在您的general操作中:
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当您经历了定义params[:store]的麻烦时,我不知道为什么要使用store_params。所以,我会做一些更像:
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
endhttps://stackoverflow.com/questions/57177241
复制相似问题