使用 :alert(或 :notice)和 render 方法,来自 Ruby On Rails 指南“Layouts and Rendering in Rails”,对我不起作用:

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15190508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:32:21  来源:igfitidea点击:

The use of :alert (or :notice) with the render method, from the Ruby On Rails guide called 'Layouts and Rendering in Rails', does not work for me:

ruby-on-railsruby-on-rails-3.2

提问by Caroline Schnapp

The use of :alert (or :notice) with the render method, from the Ruby On Rails guide called 'Layouts and Rendering in Rails' at http://guides.rubyonrails.org/layouts_and_rendering.html, does not work for me

http://guides.rubyonrails.org/layouts_and_rendering.html 的名为“Layouts and Rendering in Rails”的 Ruby On Rails 指南中,将 :alert(或 :notice)与渲染方法一起使用对我不起作用

That's the sample code provided in the guide:

这是指南中提供的示例代码:

    def index
      @books = Book.all
    end

    def show
      @book = Book.find_by_id(params[:id])
      if @book.nil?
        @books = Book.all
        render "index", :alert => 'Your book was not found!'
      end
    end

I have a hello controller that looks like this:

我有一个 hello 控制器,如下所示:

    class HelloController < ApplicationController
      def index
        @counter = 5
      end
      def bye
        @counter = 4
        render "index", :alert => 'Alert message!'
      end
    end

My index.html.erb view looks like that:

我的 index.html.erb 视图如下所示:

    <ul>
    <% @counter.times do |i| %>
      <li><%= i %></li>
    <% end %>
    </ul>

When accessing http://localhost:3000/hello/bye, I see the index view, i.e. a list of numbers from 1 to 4 as expected, but there's no 'Alert message!' alert showing.

访问时http://localhost:3000/hello/bye,我看到了索引视图,即按预期从 1 到 4 的数字列表,但没有“警报消息!” 警报显示。

My layout uses this to show alert messages:

我的布局使用它来显示警报消息:

    <% flash.each do |k, v| %>
      <div id="<%= k %>"><%= v %></div>
    <% end %>

回答by Dan Wich

I'm confused as to why that Rails Guide mentions using flash values in render, since they only appear to work in redirect_toat the moment. I think you'll find your approach works if you put a flash.now[:alert] = 'Alert message!'before your render method call.

我很困惑为什么 Rails 指南提到在 中使用 flash 值render,因为它们目前似乎只适用redirect_to。如果您flash.now[:alert] = 'Alert message!'在渲染方法调用之前放置一个,我认为您会发现您的方法有效。

Edit: this is a flaw in the guides that will be fixed, you should use the separate method call to set the flash prior to calling render.

编辑:这是将被修复的指南中的一个缺陷,您应该在调用渲染之前使用单独的方法调用来设置 Flash。

回答by PericlesTheo

Try

尝试

  def bye
    @counter  = 4
    flash.now[:error] = "Your book was not found"
    render :index
  end

回答by Karl Wilbur

Normally you would do something like:

通常你会做这样的事情:

if @user.save
  redirect_to users_path, :notice => "User saved"
else
  flash[:alert] = "You haz errors!"
  render :action => :new
end

What you want to do is (and I like this syntax much better):

你想要做的是(我更喜欢这种语法):

if @user.save
  redirect_to users_path, :notice => "User saved"
else
  render :action => :new, :alert => "You haz errors!"
end

...however, that isn't valid for ActionController::Flash#render.

...但是,这不适用于ActionController::Flash#render.

But, you can extend ActionController::Flash#renderto do exactlywhat you want:

但是,您可以扩展ActionController::Flash#render完全执行您想要的操作:

Create config/initializers/flash_renderer.rbwith the following content:

config/initializers/flash_renderer.rb使用以下内容创建:

module ActionController
  module Flash

    def render(*args)
      options = args.last.is_a?(Hash) ? args.last : {}

      if alert = options.delete(:alert)
        flash[:alert] = alert
      end

      if notice = options.delete(:notice)
        flash[:notice] = notice
      end

      if other = options.delete(:flash)
        flash.update(other)
      end

      super(*args)
    end

  end
end

Ref: http://www.perfectline.co/blog/2011/11/adding-flash-message-capability-to-your-render-calls-in-rails-3/

参考:http: //www.perfectline.co/blog/2011/11/adding-flash-message-capability-to-your-render-calls-in-rails-3/