[英]No route matches [POST] “/account/new”
我正在尝试创建一个新帐户。 但是由于某种原因,当我尝试提交表单No route matches [POST] "/account/new"
时遇到此错误。 这是我的form_for:
<%= form_for @account, url: new_account_path, html: { method: :post } do |form| %>
account_index GET /account(.:format) account#index
POST /account(.:format) account#create
new_account GET /account/new(.:format) account#new
edit_account GET /account/:id/edit(.:format) account#edit
account GET /account/:id(.:format) account#show
PATCH /account/:id(.:format) account#update
PUT /account/:id(.:format) account#update
DELETE /account/:id(.:format) account#destroy
resources :account
class AccountController < ApplicationController
def new
@account = Account.new
end
def create
binding.pry
interactor = CreateAccount.call(
params: account_params,
user: current_user
)
if interactor.success?
redirect_to pages_dashboard_path
else
render :new
end
end
private
def account_params
params.require(:account).permit(:name)
end
end
<div class="plans">
<h1>New Account Plan</h1>
<%= form_for @account, url: new_account_path, html: { method: :post } do |form| %>
<div class="plans__trial">
<h3>Trial</h3>
<p>1 Worker</p>
<p>1 Assignment</p>
<p class="price">Price: Free</p>
<label>
<%= form.submit "Select", name: "trial", class: "button" %>
</label>
</div>
<div class="plans__tier-1">
<h3>Tier 1</h3>
<p>Unlimited Workers</p>
<p>Unlimited Assignments</p>
<p class="price">Price: $9.00</p>
<label>
<input type="radio" required />
<a href="" class="button">Select</a>
</label>
</div>
<div class="cc-fields">
<%= render partial: "shared/cc-fields" %>
</div>
<%= form.submit name: "tier_1" %>
<% end %>
</div>
我究竟做错了什么。 它应该击中创建动作吗? 目前不是。
就在您的路线上。
你POST
荷兰国际集团的NEW
途径。 那不行 新建是您呈现供用户输入数据的表单的位置。 创建是将接受发布到其上的数据的操作。
所以改变这个:
<%= form_for @account, url: new_account_path, html: { method: :post } do |form| %>
对此:
<%= form_for @account, url: account_path, html: { method: :post } do |form| %>
简化:
<%= form_for @account do |form| %>
只要您的@account是一个Account.new,它就应该起作用。
尝试在routes.rb中更改此代码
resources :account
至:
resources :accounts
那么你可以使用这段代码
<%= form_for @account, url: new_account_path, html: { method: :post } do |form| %>
将form_for
line更改为此,如果您遵循Convention Over Configuration,rails将自动处理路由。
<%= form_for @account do |form| %>
希望有帮助!
我有一个类似的问题。 我在类似的帖子上尝试了所有答案,但没有成功,包括上面的答案。
我什至删除了该应用程序并重复了该过程,并陷入了同一问题。
沮丧之后,我意识到它与text_area字段有关。 在每种情况下,我都将网页中的随机文本复制到引发错误的正文文本字段中。 我知道这一点是因为我决定改为输入随机文本,瞧! 它在提交时起作用。
一直没有错。 由于Ruby的text_area属性没有限制 ,我想它必须与我要复制的字符一起在后台处理代码,因为它会将无法理解的内容转换为HTML,如下所述。
这是我自己的特殊情况,我认为我应该分享。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.