Udemy:训练营课程:部分:8讲座:122:保存到DATABSE:在“联系我们”页面中,我填写了信息,名称:test1,电子邮件:test1@example.com,评论:test1,as一个测试,按下提交,但按钮不提交信息,并没有给出错误消息。我哪里错了?有专家意见吗?这是我的代码:
class ContactsController < ApplicationController
def new
@contact = Contact.new
end
def create
@contact = Contact.new(contact_params)
if @contact.save
flash[:success] = 'Message sent.'
redirect_to new_contact_path
else
flash[:danger] = 'Error occured, message has not been sent.'
redirect_to new_contact_path
end
end
end
private
def contact_params
params.require(:contact).permit(:name, :email, :comments)
end
<div class="row">
<div class="col-md-4 col-md-offset-4">
<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.email_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>
答案 0 :(得分:0)
我不是铁杆专家,但您可以尝试使<%= form_for(@contact, :method => :get) do |f| %>
能够让它发挥作用。
在contact.rb
模型中输入以下内容
class Contact < ActiveRecord::Base
end