【问题标题】:No route matches [POST] "/account/new"没有路线匹配 [POST] "/account/new"
【发布时间】:2017-04-02 03:05:01
【问题描述】:

我正在尝试创建一个新帐户。但由于某种原因,我在尝试提交表单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

ROUTES.RB

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

NEW.HTML.ERB

<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>

我做错了什么。它应该击中创建动作吗?目前没有。

【问题讨论】:

    标签: html ruby-on-rails


    【解决方案1】:

    form_for 改成这样,如果你遵循Convention Over Configuration,rails 会自动处理路由。

    <%= form_for @account do |form| %>
    

    希望有帮助!

    【讨论】:

      【解决方案2】:

      就在您的路线中。

      您正在 POSTing 到 NEW 路线。那是行不通的。新是您呈现表单供用户输入数据的地方。 Create 是接受发布到它的数据的操作。

      所以改变这个:

      <%= 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,它就是这样,它应该可以工作。

      【讨论】:

      • 我收到这个错误代码No route matches {:action=&gt;"show", :controller=&gt;"account"} missing required keys: [:id]
      • 现在我收到了这个错误undefined method accounts_path' #:0x007fa9e4e1f5b0>`
      • 从路径中删除 s。应该是 account_path
      【解决方案3】:

      尝试在 routes.rb 中更改此代码
      resources :account
      至:
      resources :accounts
      那么你可以使用这个代码
      &lt;%= form_for @account, url: new_account_path, html: { method: :post } do |form| %&gt;

      【讨论】:

        【解决方案4】:

        我遇到了类似的问题。我在类似帖子上尝试了所有答案,但没有任何效果,包括上面的那些。

        我什至删除了该应用程序并重复了该过程并陷入了同样的问题。

        经过一番挫折,我意识到这与 text_area 字段有关。在每种情况下,我都将网页中的随机文本复制到引发错误的正文文本字段中。我知道这一点是因为我决定输入随机文本,瞧!它在提交时起作用。

        一直没有错。由于Ruby text_area attribute has no limit,我想它必须与我正在复制的字符一起在后台弄乱代码,因为它将它不理解的内容转换为HTML,如此处所述。

        这是我自己的特殊情况,我想我应该分享一下。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-01-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多