【问题标题】:Ruby on Rails: No routes match for[POST] '/contacts/new'Ruby on Rails:没有路线匹配 [POST] '/contacts/new'
【发布时间】:2016-12-12 22:23:05
【问题描述】:

Rails 5. 不断收到找不到路线的错误。我在这里做错了什么?

App/views/contacts/new.html.erb

<%= form_for "@contact" do |f| %>
                <div class="form-group">
                    <%= f.label :name %>
                    <%= f.text_field :name, class: 'form-control' %>
                </div>

                <div class="form-group">
                    <%= f.label :email %>
                    <%= f.text_field :email, class: 'form-control'  %>
                </div>

                <div class="form-group">
                    <%= f.label :comments %>
                    <%= f.text_area :comments, class: 'form-control' %>
                </div> 

                <%= f.submit 'Submit', class: 'btn btn-default' %>

     <% end %>

routes.rb 文件

Rails.application.routes.draw do
root to: 'pages#home'
get '/about', to: 'pages#about'
resources :contacts
end

contacts_controller.rb

class ContactsController < ApplicationController
def new
    @contact = Contact.new
end

def create
    @contact = Contact.new(contact_params)
    if @contact.save
        redirect_to new_contact_path, notice: "Message sent."
    else
        redirect_to new_contact_path, notice: "Error occured."
    end
end

private
    def contact_params
        params.require(:contact).permit(:name, :email, :comments)
    end

结束

contact.rb(模型文件,暂时空白)

class Contact < ActiveRecord::Base
end

我不知道为什么。提前致谢。

【问题讨论】:

  • 我很好奇它为什么要发出帖子请求。你在做什么会触发发布请求?提交表单时会发生这种情况吗?
  • 是的。这是一个正在提交的表单,通常在提交时会自动发送一个 POST 请求。

标签: ruby-on-rails ruby model-view-controller routes


【解决方案1】:

您的表单中的@contact 变量有引号,请尝试使用普通的@contact 变量。

【讨论】:

  • 非常感谢!那解决了它。不敢相信我浪费了几个小时试图弄清楚为什么它不起作用。我是个白痴哈哈
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多