【问题标题】:No route matches [POST] "/contacts/new" Ruby on Rails没有路线匹配 [POST] "/contacts/new" Ruby on Rails
【发布时间】:2017-02-01 13:59:31
【问题描述】:

我正在使用在线教程通过 Cloud9 构建一个交互式网站。我们使用 bootstrap、JavaScript、ruby on rails、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

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

我完全按照说明进行操作,但我不知道出了什么问题或要更改什么。在我把头发扯掉之前有人可以帮忙吗?

【问题讨论】:

    标签: ruby-on-rails twitter-bootstrap ruby-on-rails-3 cloud9-ide


    【解决方案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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多