Victor
Victor

Reputation: 1205

Flash Message (Rails v.4.1.1) Curly Brackets

I'm creating a blog on rails. Everytime a post is created on the new page I want to be back at the index page and see a flash message saying "You post was saved". On the layout I have the following:

<body>

<%= flash.each do |key, value| %>
    <p><%= value %></p>
<% end %>


<%= yield %>

</body>

The problem is that I have now on every page (index, new, etc.) curly brackets on the top and I don't know why. Also, instead of just having a message "Your post was saved.". It also appears the following: "{"notice"=>"Your post was saved."}". This is the code for the message hold on the controller (posts_controller.rb):

def create
        @post = Post.new(post_params)

        if @post.save
            redirect_to posts_path
            flash[:notice] = "Your post was saved."
        else
            render "new"
        end
end

I'm beginning with Rails, thanks for the help.

Upvotes: 1

Views: 386

Answers (1)

Justin
Justin

Reputation: 4940

See update below for explanation

remove the = in <%= flash.each.... %> Should just be <% flash.each.....%>

<% flash.each do |key, value| %>
  <p><%= value %></p>
<% end %>

To keep it simple, when you want your ruby code to render something onto the page, use <%= %>, ex: to show the current time you would use <%= Time.now %>. For other things like settings variables in your view without text rendering on the page, use <% %>.

For example, say I want to assign a variable equal to the current time so I can use it below in the view, I would use <% time = Time.now %> then I can show that time on the page with <%= time %>.

<% time = Time.now %>

<p>The current time is <%= time %>.</p>

Upvotes: 4

Related Questions