繁体   English   中英

没有路线与[POST]“ / contacts / new” Ruby on Rails匹配

[英]No route matches [POST] “/contacts/new” Ruby on Rails

我正在使用在线教程通过Cloud9构建一个交互式网站。 我们正在使用引导程序,JavaScript,rails上的ruby,html和scss。 但是,我目前陷入困境。 每当我点击“提交” ...时,我就会看到“ Routing Error page 没有任何信息存储在我的数据库中。

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>

我完全按照说明进行操作,不知道什么是错误的或要更改的内容。 在我扯掉头发之前有人可以帮忙吗?

你需要改变

<%= 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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM