【发布时间】:2016-12-12 22:23:05
【问题描述】:
Rails 5. 不断收到找不到路线的错误。我在这里做错了什么?
App/views/contacts/new.html.erb
<%= 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 %>
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
结束
contact.rb(模型文件,暂时空白)
class Contact < ActiveRecord::Base
end
我不知道为什么。提前致谢。
【问题讨论】:
-
我很好奇它为什么要发出帖子请求。你在做什么会触发发布请求?提交表单时会发生这种情况吗?
-
是的。这是一个正在提交的表单,通常在提交时会自动发送一个 POST 请求。
标签: ruby-on-rails ruby model-view-controller routes