我正在尝试创建一个新帐户。但由于某些原因,我在尝试提交表单 Error: Unexpected data type
时收到此错误。这是我的form_for:
No route matches [POST] "/account/new"
<%= 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
我做错了什么。它应该击中创建动作吗?它目前不在。
答案 0 :(得分:0)
你的路线正确。
您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,它就应该有效。
答案 1 :(得分:0)
尝试在routes.rb中更改此代码
resources :account
于:
resources :accounts
那么你可以使用这个代码
<%= form_for @account, url: new_account_path, html: { method: :post } do |form| %>
答案 2 :(得分:0)
将form_for
行改为此行,如果您按照约定优先配置,rails将自动处理路由。
<%= form_for @account do |form| %>
希望有所帮助!
答案 3 :(得分:0)
我有类似的问题。我尝试了类似帖子的所有答案,但没有任何效果,包括上面的那些。
我甚至删除了该应用并重复了该过程并陷入了同样的问题。
非常沮丧之后,我意识到这与 text_area 字段有关。在每种情况下,我都将网页中的随机文本复制到正在抛出错误的正文文本字段中。我知道这是因为我决定改为输入随机文字而瞧!它致力于提交。
一直没有错。自Ruby text_area attribute has no limit以来,我想它必须与我在复制背景中的代码混乱,因为它将它不理解的内容转换为HTML,如此处所述。
这是我自己的特例,我认为我应该分享。