Simple_Form和Bootstrap 3表单验证通知已停止工作

时间:2013-09-26 07:03:35

标签: ruby-on-rails-3 twitter-bootstrap simple-form

我还在学习RoR并且已经在线学习了各种教程。在那个过程中,我相信我搞砸了Bootstrap 2在验证Simple_Form提交时显示的漂亮的Flash通知。我试图更新我的代码,但没有成功。这是我到目前为止所拥有的......

运行: Rails 3.2.13 Ruby 2.0.0

我刚刚在 gemfile 中使用此gem升级到Bootstrap 3:

gem 'bootstrap-sass-rails'

application.html.erb 中,我有:

<%= render 'layouts/messages' %>

_messages 部分我有:

<% flash.each do |type, message| %>
  <div class="alert <%= bootstrap_class_for(type) %> fade in">
    <button class="close" data-dismiss="alert">×</button>
    <%= message %>
  </div>
<% end %>

在我的 application_helper.rb 中,我有:

def bootstrap_class_for flash_type
    case flash_type
      when :success
        "alert-success"
      when :error
        "alert-error"
      when :alert
        "alert-block"
      when :notice
        "alert-info"
      else
        flash_type.to_s
    end
  end

在我的 users_controller.rb 中,我有:

def update
    respond_to do |format|
      if @user.update_attributes(params[:user])
        format.html { redirect_to @user, notice: 'Account successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

在我的 edit.html.erb 视图中,我有:

<%= simple_form_for(@user) do |f| %>
    <%= f.input :firstname %>
    <%= f.input :lastname %>
    <%= f.input :email %>
    <%= f.input :password %>
    <%= f.input :password_confirmation %>
    <%= f.submit "Save changes", class: "btn btn-lg btn-primary" %>
<% end %>

验证有效,但返回编辑视图时,不会显示格式(红色表示错误)或闪存消息。只显示每个字段外很难发现的消息。我必须错过Simple_Form和Bootstrap 3之间的一些联系,只是不知道是什么。

我发现海报建议添加的另一篇文章:

config.input_class = "form-control"

到我的simple_form初始化程序,但这给了我一个错误(想想我可能没有最新版本?):

undefined method `input_class=' for SimpleForm:Module (NoMethodError)

我希望我知道发生了什么,但我真的希望有人能帮助我恢复格式化和flash消息。抱歉,如果这是一个全新的问题,但我感到有点迷茫,可能后悔我很快就升级到Bootstrap 3。

提前感谢所有阅读这一切的人:)

2 个答案:

答案 0 :(得分:4)

我从railscasts.com和其他网站获得了以下代码组合。

<% flash.each do |name, msg| %>
<div class="alert alert-<%= name == :notice ? "success" : "error" %>">
   <a class="close" data-dismiss="alert">×</a>
      <%= msg %>
</div>
<% end %>

将其添加到控制器的顶部:

respond_to :html, :json

将它放在每个控制器动作中:

def create
  ...
  flash[:notice] = 'User was successfully created.'
  respond_with(@user)
end

适用于rails 3.2+和4.0以及twitter bootstrap rails 2,在tbsr 3中未经测试但可能正常工作。

答案 1 :(得分:0)

这对我来说比其他人好得多;因为它更短,包括所有警报案例 - &gt; 错误,成功,提醒&amp;通知由Bootstrap提供。

N.B:我将_messages重命名为_flash_messages,因为这是大多数人使用的。

对于那些可能想知道在哪里生成_flash_messages部分的人来说,这很简单,只需右键单击视图中的布局文件夹和& #39;添加新文件&#39;。您可以将其称为_flash_messages,并确保在闪存之前有第一个下划线(_)...&#39;。点按“保存”

现在在 _flash_messages.html.erb 部分中,使用此

    <% unless flash.blank? %>
       <% flash.each do |type, message| %>
          <div class="alert <%= flash_class(type.to_s) %>">
             <button class="close" data-dismiss="alert">x</button>
          <%= message %>
          </div>
       <% end %>
    <% end %>

application_helper.rb 中使用此

       def flash_class (type)
          case type
             when 'error'
                 "alert-error"
             when 'notice'
                 "alert-info"
             when 'alert'
                 "alert-block"
             when 'success'
                 "alert-success
          else
             type.to_s
          end
     end

然后在 application.html.erb 中,将其添加为第一个div中的第一行

<%= render partial: 'layouts/flash_messages', flash: flash %>

请注意,我们渲染部分(任何部分),而没有&#39; starter&#39;下划线(_),即在&#39; flash ...&#39;

之前

您可以在显示不同警报案例时从teamtreehouse查看此答案。

希望这有帮助!