Simple_Form和引导程序 3 表单验证通知停止工作



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

运行:导轨 3.2.13红宝石 2.0.0

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

gem 'bootstrap-sass-rails'

在我的应用程序.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

在我的编辑.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)

我希望我知道发生了什么,但我真的希望有人可以帮助我恢复格式和闪存消息。抱歉,如果这是一个完全的新手问题,但我觉得有点迷茫,可能后悔我过早升级到 Bootstrap 3

提前感谢任何阅读所有这些:)的人

我从 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 测试,但可能工作正常。

这对我来说比其他的要好得多,因为它更短,包括所有警报案例 ->错误,成功,警报和Bootstrap提供的通知

注意:我把你的_messages改名为偏_flash_messages,因为这是大多数人使用的。

对于那些可能想知道在哪里生成_flash_messages部分的人来说,这很容易,只需右键单击视图中布局文件夹并"添加新文件"。你可以像我一样_flash_messages称呼它,并确保你在"flash..."之前有第一个下划线(_(。点击"保存">

现在在您的 _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

然后在您的应用程序 .html.erb 中,将其添加为第一个div 的第一行

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

请注意,我们渲染部分(即任何部分(,没有"starter"下划线(_(,即在单词"flash"之前。

您可以从teamtreehouse查看有关显示不同警报案例的答案。

希望这有帮助!

最新更新