0

Ich erstelle eine interaktive Website über Cloud9 mit einem Online-Lernprogramm. Wir verwenden Bootstrap, JavaScript, Ruby on Rails, HTML und Scss. Allerdings stecke ich derzeit fest. Immer wenn ich auf "Senden" klicke, bekomme ich eine Routing Error page. Keine der Informationen ist in meiner Datenbank gespeichert.Keine Route stimmt mit [POST] "/ contacts/new" überein 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 

Kontakte/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> 

Ich folgte die Anweisungen genau, und ich habe keine Ahnung, was falsch ist oder was zu ändern ist. Kann mir jemand helfen, bevor ich mir die Haare reibe?

Antwort

1

Sie benötigen

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

zu

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

Voll Code

<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 danke Ihnen so sehr, @Deepak zu ändern! – Andrea

Verwandte Themen