0

我正在使用在線教程通過Cloud9構建交互式網站。我們使用bootstrap,JavaScript,ruby on rails,html和scss。但是,我目前卡住了。每當我點擊'提交'...我得到一個Routing Error page。沒有任何信息存儲在我的數據庫中。沒有路線匹配[POST]「/ contacts/new」Ruby on Rails

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

觸點/ new.html.erb

<div class="container"> 
    <div class="row"> 
    <h3 class="text-center">Contact Us</h3> 
    <div class="col-md-4 col-md-offset-4"> 
     <%= flash[:notice] %> 
     <div class="well"> 
     <%= 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 %> 
     </div> 
    </div> 
    </div> 
</div> 

我跟着準確的說明,我不知道什麼是錯的或改變什麼。在我拔掉頭髮之前有人能幫忙嗎?

回答

1

您需要更改

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

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

的完整代碼

<div class="container"> 
    <div class="row"> 
    <h3 class="text-center">Contact Us</h3> 
    <div class="col-md-4 col-md-offset-4"> 
     <%= flash[:notice] %> 
     <div class="well"> 
     <%= 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 %> 
     </div> 
    </div> 
    </div> 
</div> 
+0

OMG太感謝你了,@Deepak! – Andrea