Helpers Outside Views in Rails 5

In this article, we will the changes required to get the original railscast on using helpers outside views working in Rails 5. Here is the create action in products controller:

  def create
    @product = Product.new(allowed_params)
    if @product.save
      flash[:notice] = "Successfully created product #{view_context.link_to('product', @product)}."
      redirect_to @product
    else
      render :new
    end
  end

Use html_safe in the application.html.erb.

<%- flash.each do |name, msg| -%>
  <%= content_tag :div, msg.html_safe, :id => "flash_#{name}" %>
<%- end -%>

You will now see the link to the product in the flash message. You can download the source code from https://github.com/bparanj/sto.re.


Related Articles