Rails 3.1 Devise How To Change Flash Message CSS From notice to success? - ruby-on-rails-3.1

Rails 3.1 and Devise 1.5 question.
I'm using the following code to display flash messages in my layout:
<% flash.each do |key, message| %>
<%= content_tag(:div, message, :class => "flash #{key}") %>
<% end %>
I'd like to change the css class for some of my confirmation messages from notice to success, but I don't know where to override or change the key because I don't know where it's set.
Can anybody point me in the right direction?
Thanks!

So the way to do this is to edit the devise controllers.
When you install Devise through the normal installation I don't think it install the controllers (at least it didn't for me).
So first you should manually add the controller files and put them into your project in the same location: https://github.com/plataformatec/devise/tree/master/app/controllers/devise
Once you have the files in your project, go to the "sessions_controller.rb" file.
One lines 16 & 25, you should see the code:
set_flash_message :notice, :signed_in
and
set_flash_message :notice, :signed_out
You just need to change ":notice" to ":success"
Hope that works!

I ran into this problem because I'm using the bootstrap-sass gem which uses a whole different class structure for flash messages. I took the route of solving this with sass rather than messing with devise at all (sass inheritance is awesome:).
So, I render the flash messages in app/view/layouts/application.html.haml:
%div.container
- flash.each do |key, value|
= content_tag(:div, value, class: "alert alert-dismisable alert-#{key}")
And, I add a simple rule to app/assets/stylesheets/custom.css.scss to get Devise's flash messages working:
/* Style Devise Flash messages like Bootstrap */
.alert-alert {
#extend .alert-warning;
}
That's it! now flash[:alert] is styled just like bootstrap's flash[:warning].

The css class is being set by the :class argument. To add a class of notice (or success, etc.), just change your call to the following:
<%= content_tag(:div, message, :class => "flash #{key} notice") %>

Related

Unable to implement recaptcha gem options in a Rails 4.2 app

I am using the recaptcha gem version 4.0, and therefore placed <%= recaptcha_tags %> inside a form. There are options that you are supposed to be able to implement to change the size (normal or compact?) and theme (light or dark).
I reviewed numerous posts here and elsewhere without finding a syntax that works. Here are several options I used based on what I found:
<%= recaptcha_tags :display => {:size => 'compact', :theme => 'dark'} %>
<%= recaptcha_tags :display => {size: 'compact', theme: 'dark'} %>
<%= recaptcha_tags :size => 'compact'} %>
<%= recaptcha_tags size: 'compact'} %>
Nove of these worked. I also removed the options from the recaptcha tags and modified the gem's client_helper.rb file. I cut all of the width and height styles were cut in half. That didn't work either. I'd rather use the options that can be supplied in the recaptcha tags. Anybody know how to do that? Thanks in advance! Jay
check the generated tag, if you pass size: 'compact' it should create a data-size="compact" attribute
https://github.com/ambethia/recaptcha/blob/master/lib/recaptcha/client_helper.rb#L70-L71
if that does not work, update to latest ... maybe bundle open recaptcha ... if size should not be data-size but regular size then this might be a bug and needs a proper fix ... still open the gem, turn it remove :size from the list and see if that fixes it
--retransmitted from grosser

Is there a good rails gem to output the view equivalent of rake routes?

What I'm looking for is a gem that could output a complete webpage version of each view a rails app has.
I realize I might not have correctly worded this. What I mean is I would like to see each route that has html output, but what the view actually looks like, not just a line saying what the view is.
In development (rails 4.x) you can simply go to any non-existent page and it will return all of the routes.
Example Screenshot: https://dl.dropboxusercontent.com/u/23115266/Screen%20Shot%202014-02-27%20at%209.21.03%20AM.png
The sextant gem allows you to see all your application's views in development by navigating to the /rails/routes URL.
https://github.com/schneems/sextant
Please correct me if I'm wrong, but it looks like you want a listing of view templates. Not sure there is a gem that can do this, but you could easily do it yourself with the following:
module ShowViewsHelper
def all_views_from_path(path)
helpers = Array(path).flat_map do |_path|
extract = /^#{Regexp.quote(_path.to_s)}\/?(.*)_.erb$/
names = Dir["#{_path}/**/*.erb"].map { |file| file.sub(extract, '\1') }
names.sort!
end
helpers.uniq!
helpers
end
end
create a template somewhere with the following:
<ul>
<% all_views_from_path("app/views").each do |v| %>
<li><%= v %></li>
<% end %>
</ul>

Rails, Twitter Bootstrap, Simple Form, Heroku - Odd error when using ":wrapper => :prepend do" to get an add-on span

I have the following code in a form partial:
<%= f.association :client %>
<%= f.input :url, :wrapper => :prepend do %>
<%= content_tag :span, "http://www.", :class => "add-on" %>
<%= f.input_field :url, :class => 'span4' %>
<% end %>
Trying to load anything to do with this form partial e.g. new/edit etc causes Heroku to error. It works flawlessly on localhost.
The only obvious error in the Heroku logs is:
ActionView::Template::Error (undefined method `source' for nil:NilClass):
I'm afraid I don't really understand that but thought it might help anyone trying to answer this.
If I change the form partial code it works on Heroku no problem at all e.g.
<%= f.input :url, :input_html => {:class => 'span4' } %>
Obviously I lose my nice bootstrap add-on span so I'd rather find out what the issue is rather than running around it.
Shot in the dark, but maybe an initializer that sets up the prepend wrapper (like initializers/simple_form.rb or initializers/simple_form_bootstrap.rb) is not being run by Heroku, either because it's not checked into git, or because of some environment-specific logic in the initializers or environment files.
Well after testing and some help from Dan I finally got to the bottom of it. Unfortunately, it seems to be quite localised to my situation though.
But just in case anyone stumbles across this, the error I had was the following:
ActionView::Template::Error (undefined method `source' for nil:NilClass):
That was in the log and was THE SAME when I followed Dan's advise to turn on error reporting on the production site (set consider_all_requests_local = true in config/environments/production.rb).
I thought that the error was being caused in the template, however, it was some validation rules in the model. The validation rules for me where some regex to stop the user starting the input with a certain string. They worked perfectly fine on my local, but Heroku really wasn't liking it!
I removed those validation rules and voila, my nice bootstrap add-on span was working perfectly.

Remote Method Understanding?

In rails 3
While using remote method form submit, it affects the database twice with the same form values.
controller file:
#user_message = UserMessage.new(params[:user_message])
if #user_message.save
render :update
else
render :nothing => true
end
view file:
<%= form_tag ('/feedback/user_message'), :method =>'post', :remote=> true, :id=>'user_message' do%>
<%= hidden_field_tag 'user_message[user_id]', #user.user_id %>
<h2><%= #question %></h2>
<%= text_area_tag 'user_message[msg]',"", :size=>"40x5" %>
<%= submit_tag "Submit"%>
<% end %>
When I hit the submit button it creates two records on the table.
Why?
There may be couple of things that might be causing this
may be the ujs file is included twice may may be like
//= require jquery_ujs // expected to load from the rails-jquery gem
//= require_tree . // if any file is present in assets directory hierarchy it will be loaded
Or just a bug taking your sleep away
https://github.com/rails/jquery-ujs/issues/208
check out more solutions
Jquery Rails 3... form submits twice... deletes twice... help
Rails 3.1 remote requests submitting twice
This may also be related to the understanding of asset pipeline when and how use pecompiled assets.
I always use this config in development mode
config.server_static_assets = false
this forces the app to call the assets from app assets
and use precompiled assets from public in production mode
The problem most likely related to asset pipeline
You should precompiled the asset pipeline before.
This will create two copy of rails.js, one in your assets and one in application.js
This is a bug or gotcha in rails 3
See here http://www.ruby.code-experiments.com/blog/2011/10/another-gotcha-with-the-rails-31-asset-pipeline-or-why-are-my-jquery-ujs-ajax-requests-triggered-twi.html
Hope this help

Rails 3 - link/button to run ajax request without redirecting

What's the best way for a link to run an ajax command (I'm using jquery in Rails 3.1)? So far I am trying to use a link to run code via the controller action "update_me."
views/products/new.html.erb has the following line:
<%= link_to "Update", :action => 'update_me' %>
In my products controller, I have:
def update_me
logger.debug 'ajax code will be here'
end
But this gives a missing template error:
Template is missing
Missing template products/update_me, application/update_me with {:handlers=>[:erb, :builder, :coffee], :formats=>[:html], :locale=>[:en, :en]}. Searched in: * "/home/ubuntu/code/preevio/app/views" * "/home/ubuntu/.rvm/gems/ruby-1.9.2-p290#rails31/gems/devise-1.4.3/app/views"
Why do I need to have a update_me.html.erb in my views? I assume that's what they want. Can't I just launch the code in the controller action/method without having to have a view for it? Or am I approaching this the wrong way?
I know this answer is a bit late, but I post it for anyone else who may get here trying to do something similar.
If you change your line of html to
<%= link_to "Update", :action => 'update_me', :remote => true %>
That should have the link to the provided action and do the ajax call for you.
You can look at the provided link to see the docs.
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
Since Rails has convention over configuration, Rails expects an appropriate view for the action in the controller.
If you want to render nothing:
http://guides.rubyonrails.org/layouts_and_rendering.html#using-render
render :nothing => true

Resources