路由错误没有路由匹配[POST]“/ contacts / new

时间:2017-02-24 15:22:14

标签: ruby-on-rails-5

我似乎陷入了Ruby on Rails教程。我一直收到这篇文章标题中的错误,即:

路由错误没有路由匹配[POST]“/ contacts / new”

应用程序/视图/联系人/ new.html.erb

<div class="container">
<div class="rpw">
    <h3 class="text-center">Contact Us</h3>

    <div class="col-md-4 col-md-offset-4">
        <%= flash[:notice] %>
        <div class="well">
            <%= form_for "/contacts" 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-success' %>
        <% end %>
    </div>
    </div>
</div>

的routes.rb

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

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

我不确定我哪里出错了。请帮我。我一直试图解决这个问题几天。

1 个答案:

答案 0 :(得分:1)

在new.html.erb

尝试更改

    <%= form_for "/contacts" do |f| %>

     <%= form_for @contact do |f| %>