2017-02-24 86 views
1

我似乎被卡在Ruby on Rails的教程中,我不斷收到這是在標題這篇文章的,這是錯誤:路由錯誤的路由匹配[POST]「/聯繫人/新

路由錯誤沒有路由匹配[POST] 「/聯繫人/新」

應用程序/視圖/聯繫人/ 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 

我不確定我在哪裏出錯了。請幫幫我。我一直在嘗試解決這個問題幾天。

+0

試<(%)=的form_for @contact做| F | %> –

回答

1

在new.html.erb

嘗試更改

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

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

非常感謝。這工作。 –

+0

沒問題。不知道你從哪裏得到'/ contacts',但是你在你的控制器裏用'new'方法定義了'@ contact'。這意味着在new.html.erb中這個變量是可用的。 –

相關問題