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