我正在cloud9 IDE
的{{1}}申请中工作。我在“联系我们”表单页面的fields: Name, Email, Comments
中输入了测试信息,点击了提交,并在地址栏中显示以下信息,但未发送消息'确认:
我在字段中没有输入任何信息(空白),点击提交,空白信息显示在地址栏中,再次没有'错误'确认信息:
我在控制台中输入bundle exec rails c
和Contact.all
,此处未列出任何条目,且没有条目保存到数据库中:
2.3.0:001> Contact.all
联系负载(2.9ms)SELECT"联系人"。* FROM"联系人"
> => #<ActiveRecord::Relation []>
我试图弄清楚代码中的问题,做了几处修改,但无济于事。有人可能会注意到我没有的东西。这是我的细读和建议代码:
Contacts_controller.rb
class ContactsController < ApplicationController
def new
@contact = Contact.new
end
def index
end
def create
@contact = Contact.new(contact_params)
if @contact.save
name = params[:contact][:name]
email = params[:contact][:email]
body = params[:contact][:comments]
ContactMailer.contact_email(name, email, body).deliver
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
private
def contact_params
params.require(:contact).permit(:name, :email, :comments)
end
end
的routes.rb
Rails.application.routes.draw do
resources :contacts, only:[:new, :create], path_names: { new: ""}
get '/about' => 'pages#about'
root 'pages#home'
application.html.erb
<div class="container">
<% flash.each do |key, value| %>
<div class="alert alert-success", role="alert"<% key %>"Message sent!">
<% value %>
</div>
<div class="alert alert-danger", role="alert"<% key %>"Error occured, message not sent!"><% value %>
</div>
<button type="button", class="close", data-dismiss="alert", aria-label="Close"><span aria-hidden="true"></span>×></span></button>
<%= value %>
<% end %>
</div>
<%= yield %>
答案 0 :(得分:0)
If I understand your question correctly, you're stating that the flash doesn't show on an error. :danger
is not a default flash key. You'll need to either:
Update the view containing the flash to explicitly check for :danger
, or
Change your controller to use :alert
instead of :danger
.