gpt4 book ai didi

ruby-on-rails - rails : Conditional path in form partial

转载 作者:数据小太阳 更新时间:2023-10-29 08:03:43 28 4
gpt4 key购买 nike

我有两种用于新建和编辑的表单,我想提取它们的一部分,但是,据我对 Rails 的初学者了解,它们需要不同的路径才能工作。

这是我的编辑表单:

<%= simple_form_for [@user, @wiki], url: user_wiki_path do |f| %>
<%= f.input :title %>
<%= f.input :body %>
<%= f.submit class: "btn btn-success" %>
<% end %>

这是我的新表格:

<%= simple_form_for [@user, @wiki], url: user_wikis_path, method: :post do |f| %>
<%= f.input :title %>
<%= f.input :body %>
<%= f.submit class: "btn btn-success" %>
<% end %>

我能否,如果可以,如何将它们组合成一个部分 _form.html.erb 有条件地指定路径?

当我现在尝试加入他们时,我在尝试编辑时遇到以下错误:

No route matches [PATCH] "/users/32/wikis"

这些是我的路线:

                      Prefix Verb   URI Pattern                              Controller#Action
users_index GET /users/index(.:format) users#index
users_show GET /users/show(.:format) users#show
root GET / pages#index
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
user_wikis POST /users/:user_id/wikis(.:format) wikis#create
new_user_wiki GET /users/:user_id/wikis/new(.:format) wikis#new
edit_user_wiki GET /users/:user_id/wikis/:id/edit(.:format) wikis#edit
user_wiki PATCH /users/:user_id/wikis/:id(.:format) wikis#update
PUT /users/:user_id/wikis/:id(.:format) wikis#update
DELETE /users/:user_id/wikis/:id(.:format) wikis#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
wikis GET /wikis(.:format) wikis#index
wiki GET /wikis/:id(.:format) wikis#show
charges POST /charges(.:format) charges#create
new_charge GET /charges/new(.:format) charges#new
charge PATCH /charges/:id(.:format) charges#update
PUT /charges/:id(.:format) charges#update

最佳答案

您可以将局部变量发布到局部变量中。像下面这样的东西会起作用:

new.html.erb

<%= render partial: '_form', locals: {url: user_wikis_path, method: :post} %>

编辑.html.erb

<%= render partial: '_form', locals: {url: user_wiki_path(@user, @wiki), method: :post} %>

_form.html.erb

<%= simple_form_for [@user, @wiki], url: url, method: method do |f| %>
<%= f.input :title %>
<%= f.input :body %>
<%= f.submit class: "btn btn-success" %>
<% end %>

或者您可以将@url 和@method 设置为实例变量并以这种方式访问​​它们。

Controller

def new
@method = :get
@url = user_wikis_path
...
end

def edit
@method = :post
@url = user_wiki_path(@user, @wiki)
...
end

_form.html.erb

<%= simple_form_for [@user, @wiki], url: url, method: method do |f| %>
...

或者您可以在访问当前操作的表单中设置条件,如果您在 View 中使用 action_name,它将返回当前操作的名称。

关于ruby-on-rails - rails : Conditional path in form partial,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29975134/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com