Ruby on rails RubyonRails:没有与[POST]'/联系人/新';
轨道5。继续获取找不到路由的错误。我做错了什么 App/views/contacts/new.html.erbRuby on rails RubyonRails:没有与[POST]'/联系人/新';,ruby-on-rails,ruby,model-view-controller,routes,Ruby On Rails,Ruby,Model View Controller,Routes,轨道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-
<%= 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 %>
联系人\u 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
class contacts控制器
结束
contact.rb(模型文件,暂时为空)
class联系人
我不明白为什么。提前感谢。您的表单中@contact变量有引号,请尝试使用普通的@contact变量。我很好奇为什么它会发出post请求。你在做什么来触发post请求?提交表单时是否会发生这种情况?是。这是一个正在提交的表单,通常在提交时它会自动成为一个POST请求。非常感谢!这就解决了问题。真不敢相信我浪费了几个小时想弄明白为什么它不起作用。我是个白痴哈哈
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
class Contact < ActiveRecord::Base
end