【发布时间】:2018-06-30 09:40:12
【问题描述】:
我正在为一个 ruby 应用程序创建一个联系表单,在填写表单并单击提交后,没有任何反应,没有刷新或任何事情。下面是联系人控制器文件、路由文件和与表单关联的 html 文件。
控制器文件
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
路由文件
Rails.application.routes.draw do
resources :contacts
get '/about' => 'pages#about'
root 'pages#home'
HTML 文件
<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>
【问题讨论】:
-
Rails 日志显示什么,Javascript 控制台显示什么?您能否在问题正文中提供两者的 sn-ps ?另外,欢迎使用 StackOverflow!
-
你的 routes.rb 中有
end吗? + 与@JoshBrody 相同的请求
标签: html ruby-on-rails ruby forms contacts