1

You can see here that it seems like the raw contents of my DB are being printed to the page. I can't see anywhere in my code why there would be the raw output of the db printed to the view. Here is the code for the index view:

<div class="main">
    <div="messages">
        <%=@messages.each do |t|%>
        <h2 class="subject"><%=t.subject%></h2>
        <p class="content"><%=t.content%></p>
        <% end %>
        <%=link_to "Create Message", edit_path%>
    </div>
</div>

The Create Form/View:

<div class="formWrapper">
    <%= form_for @messages do |t|%>
    <div class ="Inputs">
        <%=t.text_field :subject%><br>
        <%=t.text_area :content%>

        <div class="submit">
            <%=t.submit "Submit"%>

        </div>
        <%end%>
    </div>
</div>

The Controller:

class MessagesController < ApplicationController
    def index
        @messages=Message.all
    end
    def new
        @messages=Message.new
    end
    def create
        @messages = Message.new(message_params) 
            if @messages.save 
            redirect_to '/' 
                else 
            render 'new' 
            end
    end
    private
    def message_params
        params.require(:message).permit(:content, :subject)
    end

end

1 Answers1

3

you don't need the = here: <%=@messages.each do |t|%>, the equals sign is telling erb to show every message on the view.

<% %>

Will execute Ruby code with no effect on the html page being rendered. The output will be thrown away.

<%= %>

Will execute Ruby code and insert the output of that code in place of the <%= %>

example...

<% puts "almost" %> nothing to see here would render as

nothing to see here

however

<%= puts "almost" %> nothing to see here

would render as

almost nothing to see here

Look at <% %>(without equal) in ruby erb means?

Community
  • 1
  • 1
Santiago Suárez
  • 586
  • 7
  • 12