AJwr
AJwr

Reputation: 618

Understanding Rails rendering an error from controller

I am still new to Rails and am having trouble understanding how to render certain sections of a page conditionally. I have a button in index.html.erb as well as another partial rendered:

<%= @facade.processing_button %>
<%= render 'snap', app: @facade.app %>

Which is defined as follows:

link_to processing_path(@app.id),
  method: :post, action: :processing,
  class: 'btn btn-danger' do
    concat([
      image_tag('blah', class: 'check_icon'),
      content_tag(:span, 'Processing')
    ].join(' ').html_safe)
  end

This button calls a controller method:

def processing
  if service.upload
    # render success bar?
  else
    # render error bar?
  end
end

I would like to render something like the following pictures. In the snap partial, a section looks like this normally:

default

Once the button is clicked, if the action is successful I want to render the following green success bar:

enter image description here

It is unclear to me how to accomplish this. Should I be leveraging some form of JS/CoffeeScript? Should I add the bars to the partial as hidden by default and simply show them with JS when the action is completed?

Upvotes: 1

Views: 1324

Answers (3)

Pragya Sriharsh
Pragya Sriharsh

Reputation: 559

see Doc: https://coderwall.com/p/jzofog/ruby-on-rails-flash-messages-with-bootstrap

Step 1: Add flash code in layouts/application.html.erb file

<% flash.each do |key, value| %>
    <div class="<%= flash_class(key) %>">
        <%= value %>
  </div>
<% end %>

Step 2: Just need to quickly extend application_helper.rb with the following

def flash_class(level)
    case level
        when :notice then "alert alert-info"  
        when :success then "alert alert-success"
        when :error then "alert alert-error"
        when :alert then "alert alert-error"
     end
end

# sometimes it will not work then wrap it with single quotes. for example:  
  when 'notice' then "alert alert-success"

Step 3: Add below in controller.erb

 def processing
   if service.upload
      flash[:success] = "Processing complete!"
   else
      flash[:error] = "Something went wrong!"
   end
 end

Hope it will work :)

Upvotes: 0

claasz
claasz

Reputation: 2134

  1. link_to processing_path(@app.id), method: :post, action: :processing Using both _path and :action parameters doesn't make sense. Use only one of them
  2. You need to decide whether your button is doing a "traditional" request or an AJAX request
  3. In case of a traditional request, you can use a controller variable @success = ..., and then check this variable inside the view: <% if @success %>
  4. In case of an AJAX request, things would get a little more complicated. However, there's Rails support for "success" and "failed" AJAX responses. Take a look at https://guides.rubyonrails.org/working_with_javascript_in_rails.html#rails-ujs-event-handlers. Typically, you would show/hide certain elements on the page, depending on the server response

Upvotes: 2

SgtPepper
SgtPepper

Reputation: 468

You would need something like this on your layouts

  <% flash.each do |name, msg| %>
    <%= content_tag :div, msg, class: "alert alert-info" %>
  <% end %>

Then on your controller

def processing
  if service.upload
    flash[:notice] = "Success"
  else
    flash[:notice] = "Error"
  end
end

Take a look at this: rails 4 -- flash notice

Upvotes: 1

Related Questions