没有路线匹配 [POST] Bloc

No Route Matches [POST] Bloc

好吧,我知道有一百万个帖子,但我似乎找不到解决问题的方法。我是 rails 的新手,在经历了 bloc 的指导后才开始我的第一个个人项目。

我遇到了这个错误:

没有路线匹配[POST]“/registered_applications/new”


这是我的路线:

Rails.application.routes.draw do

  resources :registered_applications 

  devise_for :users

  get 'about' => 'welcome#about'

  root :to => 'welcome#index'
end

这是表格。单击提交时,我们遇到了错误:

I was trying <%= form_for @application do |f| %> but I was getting an error so I changed over to this. Any suggestions here would be appreciated as well.

<h3 class="roboto">Register a new application</h3>

<div class="row">
  <div class="col-md-4">
    <p>Application Registration Guidelines:</p>
    <ul>
      <li>Applications must be named.</li>
      <li>You must provide a URL.</li>
      <li>You must be signed in to add an application to your account.</li>
    </ul> 
  </div>
  <div class="col-md-8 app-reg">
    <%= form_for(:application) do |f| %>
      <div class="form-group">
        <%= f.label :name %>
        <%= f.text_field :name, class: 'form-control', placeholder: "Application name" %>
      </div>
      <div class="form-group">
        <%= f.label :URL %>
        <%= f.text_field :URL, class: 'form-control', placeholder: "Application URL" %>
      </div>
      <div class="form-group">
        <%= f.submit "Save", class: 'btn btn-primary' %>
      </div>
    <% end %>
  </div>
</div>

这是我的控制器:

Definitely a work in progress

class RegisteredApplicationsController < ApplicationController
  def index
    @applications = Application.all
  end

  def new
    @application = Application.new 
  end

  def show
    @application = Application.find(params[:id])
  end

  def edit
    @application = Application.find(params[:id])
  end

  def create 
    @application = Application.new(application_params)
    if @application.save
      flash[:notice] = "Application was saved successfully."
      redirect_to @application
    else
      flash[:error] = "There was an error creating the application. Please try again."
      render :new
    end
  end
end

private

def application_params
  params.require(:application).permit(:name, :URL)
end

如果你觉得有必要看,这里是应用模型:

class Application < ActiveRecord::Base
  belongs_to :user
end

非常感谢任何帮助。

您应该将 routes.rb 中的 resources :registered_applications 更改为 resources :applications,因为 Application 是您的模型,而 applications 是被视为资源的 table 的名称。

您收到错误消息是因为没有资源或 table 命名为 registered_applications,因为您已将 table 命名为应用程序并将模型命名为应用程序。

根据您的代码,我假设您在点击应用程序创建表单中的 post 按钮时遇到了错误,对吗?因此,您应该在表单生成中明确使用 @application 而不是 :application 作为 for 模型。正如您所写的那样,您已经尝试过了,但出了点问题:如果您具体说明具体做了什么,将有助于解决问题。

resources registered_application 不会为您创建 POST 到 registered_applications/new 的路由。只有 POST 到 registered_applications

更新: 我也不建议你命名模型,class 变量,其他重要的 variables/models 可能会被命名:就像你 Application class。这可能是痛苦的奇怪错误的根源