联系人姓名未在数据库中显示姓名
Contact name not showing name in DB
我是新手,想创建一个联系表单,但是当我检查我提交的表单时,它没有显示名称。它只是说 name ""
我在 Rails 上使用 Ruby。
我的html
长得像;
Contact Us
<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 :name, 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>
我的 controller
代码看起来像;
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
结束
可能是您的第二个文本字段有问题,它被称为 :name 两次...
更改第二个输入并重试
<%= f.text_field :name, class: 'form-control' %>
至
<%= f.text_field :email, class: 'form-control' %>
我是新手,想创建一个联系表单,但是当我检查我提交的表单时,它没有显示名称。它只是说 name ""
我在 Rails 上使用 Ruby。
我的html
长得像;
Contact Us
<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 :name, 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>
我的 controller
代码看起来像;
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
结束
可能是您的第二个文本字段有问题,它被称为 :name 两次... 更改第二个输入并重试
<%= f.text_field :name, class: 'form-control' %>
至
<%= f.text_field :email, class: 'form-control' %>