Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Teo Inke
Teo Inke
4,403 Points

Ruby scaffolding - duplicated message: "Status was successfully created"

Hello!

I've been following the Ruby tutorial to create a Rails application. However, I got stuck in this problem: using scaffold model, my confirmation message for a new status appears duplicated and actually in an inconvenient place. I have searched for this on the files and I found the message in apps/controllers/statuses_controller.rb. What is weird: I changed the text, and it altered both messages. Has anyone had the same problem before?

alt text

5 Answers

Stone Preston
Stone Preston
42,016 Points

ok looks like this is the issue (not 100% sure though)

 <div class="container">
        <p class="notice"><%= notice %></p>
        <p class="alert"><%= alert %></p>
        <%= yield %>
    </div>

you have a notice being displayed in the application layout file AND in the status show view. so you have two being shown. if you remove the one in the application layout you should only see one. (but thats only a temporary fix really since I assume it was in the application layout for a reason. so you probably dont want it gone)

Stone Preston
Stone Preston
42,016 Points

can you post the code for your status show view?

Teo Inke
Teo Inke
4,403 Points

This is the code in show.html.erb:

<p id="notice"><%= notice %></p>

<p>
  <strong>Name:</strong>
  <%= @status.name %>
</p>

<p>
  <strong>Content:</strong>
  <%= @status.content %>
</p>

<%= link_to 'Edit', edit_status_path(@status) %> |
<%= link_to 'Back', statuses_path %>
Stone Preston
Stone Preston
42,016 Points

ok now post the code in your application layout file. you might have a flash message being displayed in the application layout file in addition to this one, thus you are seeing 2 messages.

Teo Inke
Teo Inke
4,403 Points

This is layouts/application.html.erb:

<!DOCTYPE html>
<html>
<head>
  <title>Treebook</title>
  <%= stylesheet_link_tag    "application", media: "all", "data-turbolinks-track" => true %>
  <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
  <%= csrf_meta_tags %>
</head>
<body>
    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="navbar-inner">
            <div class="container">
                <a href="#" class="navbar-brand"> Treebook</a>
                <ul class="nav navbar-nav">
                    <li><%= link_to "All Statuses", statuses_path %></li>
                </ul>
            </div>
        </div>
    </div>

    <div class="container">
        <p class="notice"><%= notice %></p>
        <p class="alert"><%= alert %></p>
        <%= yield %>
    </div>

</body>
</html>

This is the concerning chunk of code in controllers/statuses_controller.erb:

def create
    @status = Status.new(status_params)

    respond_to do |format|
      if @status.save
        format.html { redirect_to @status, notice: 'Confirmation message edited' }
        format.json { render action: 'show', status: :created, location: @status }
      else
        format.html { render action: 'new' }
        format.json { render json: @status.errors, status: :unprocessable_entity }
      end
    end
  end
Teo Inke
Teo Inke
4,403 Points

Thank you, this was indeed the issue! It's been a while since I coded it, so apparently everything is still fine :)

Stone Preston
Stone Preston
42,016 Points

alright cool glad it was that simple