Reputation: 618
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:
Once the button is clicked, if the action is successful I want to render the following green success bar:
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
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
Reputation: 2134
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@success = ...
, and then check this variable inside the view: <% if @success %>
Upvotes: 2
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