提交后没有确认消息

时间:2016-10-11 14:07:07

标签: ruby-on-rails forms ruby-on-rails-3

我正在cloud9 IDE的{​​{1}}申请中工作。我在“联系我们”表单页面的fields: Name, Email, Comments中输入了测试信息,点击了提交,并在地址栏中显示以下信息,但未发送消息'确认:

two months ago

我在字段中没有输入任何信息(空白),点击提交,空白信息显示在地址栏中,再次没有'错误'确认信息:

https://manual-coder-rails/contacts?utf8=%E2%9C%93&contact%5Bname%5D=Nick&contact%5Bemail%5D=victorysupplys%40gmail.com&contact%5Bcomments%5D=Nice+web&commit=Submit

我在控制台中输入bundle exec rails cContact.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>&times;></span></button>
     <%= value %>
   <% end %>
 </div>
 <%= yield %>

1 个答案:

答案 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:

  1. Update the view containing the flash to explicitly check for :danger, or

  2. Change your controller to use :alert instead of :danger.