John
John

Reputation: 516

How do I add style and html to flash error message in my controller in Rails 4.0

I have this in my UserController.rb

           def create
                @user = User.new(user_params)
                if @user.save
                    sign_in @user

                    flash[:success] = "Welcome to the TurnKii!"

                    redirect_to @user
                    # Handle a successful save.
                else


                    redirect_to signup_url
                    flash[:error] = @user.errors.empty? ? "Error" :  @user.errors.full_messages.to_sentence

                end
            end

the code works right now, but I would like to add some styling to my flash error message, it just shows up on the top of the page as bare text in 1 sentence.

I dont mind the sentence, but I would like to add some styling to this and semantic tags as well.

Currently its showing up on the page inside a

 <div class="alert alert-danger">Error Sentence here</div>

i have tried to style that in my css, but its not rendering. I would like to have something like this(or similar) for my flash message

                    flash[:error] = @user.errors.empty? ? "Error" :  <h1 class=alert-danger >@user.errors.full_messages.to_sentence</h1>

but anytime i add anything in there, I run into errors.

Can anyone help?

Upvotes: 0

Views: 1412

Answers (1)

user4121098
user4121098

Reputation:

Try add css changes in your application.css.scss file. That will automatically loaded in all the views. But if you still want flash message in html then you can use below code.

flash[:error] = @user.errors.empty? ? "Error" :  "<h1 class='alert-danger' >#{@user.errors.full_messages.to_sentence}</h1>".html_safe

Upvotes: 3

Related Questions