【问题标题】:No Route Matches [POST] Bloc没有路线匹配 [POST] Bloc
【发布时间】:2015-05-24 07:45:46
【问题描述】:

好吧,我知道有一百万个帖子,但我似乎无法找到解决我的问题的方法。我是 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

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

我正在尝试<%= form_for @application do |f| %>,但我遇到了一个错误,所以我改用了这个。这里的任何建议也将不胜感激。

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

这是我的控制器:

绝对是一个正在进行中的工作

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

非常感谢任何帮助。

【问题讨论】:

    标签: ruby-on-rails controller routes


    【解决方案1】:

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

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

    【讨论】:

    • 控制器和匹配模型的命名没有任何限制
    • 是的,我仍然要将模型和控制器重命名为其他名称,Application 只是自找麻烦。 @RoaringStones
    【解决方案2】:

    从您的代码中,我假设您在点击应用程序创建表单中的发布按钮时会收到该错误,对吧?所以你应该在表单生成中明确使用@application 而不是:application 作为 for 模型。正如您所写的那样,您已经尝试过了,但是出了点问题:如果您指定具体做了什么,这将有助于解决问题。

    resources registered_application 不会为您创建到registered_applications/new 的POST 操作路由。仅 POST 到已注册的应用程序

    更新: 我也不建议您命名模型、类变量以及可能命名的其他重要变量/模型:就像您 Application 类。这可能是痛苦的奇怪错误的根源

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-31
      • 1970-01-01
      • 2018-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多