Flash message with html_safe from the controller in Rails 4 (safe version)

前端 未结 1 1310
耶瑟儿~
耶瑟儿~ 2021-01-08 01:46

In my controller I have the following code:

    format.html { redirect_to new_customer_url,
                notice: %Q         


        
相关标签:
1条回答
  • 2021-01-08 02:09

    Here is one possible way to solve this problem. Add a before filter to your ApplicationController which will make flash[:notice] html safe only if flash[:html_safe] is set. Then you can control when and when not to make notices html safe completely from the controller.

    before_filter -> { flash.now[:notice] = flash[:notice].html_safe if flash[:html_safe] && flash[:notice] }
    

    Then your example could be modified to this:

    format.html do
      redirect_to(
        new_customer_url,
        notice: %Q[ A customer already exists with with this shopping id. Edit this customer #{view_context.link_to("here", edit_customer_url(@duplicate))}.],
        flash: { html_safe: true }
      )
    end
    
    0 讨论(0)
提交回复
热议问题