1

I'm a beginner at rails and thus far interplating data in views has been pretty straight forward. I've been introduced to something slightly new as far as how the controllers are setup and as a result I'm not sure how one would go about getting the data to present in the view.

First controller

class PagesController < ApplicationController

  def index
    @guestbook_entry = GuestbookEntry.new
    render "welcome"
  end
end

Second controller

class GuestbookEntriesController < ApplicationController

  def create
    GuestbookEntry.create(guestbook_entry_params)
    redirect_to root_path, notice: "Thank you for your entry."
  end

  private

  def guestbook_entry_params
    params.require(:guestbook_entry).permit(:body)
  end
end

And here is the welcome.html.erb

<h1>Welcome to My Guestbook</h1>
<br>
<%= image_tag("under_construction.gif") %>

<div id="guestbook-entries">
  <p>Guestbook Entries:</p>
  <ul>
  </ul>
</div>

<%= form_for @guestbook_entry do |f| %>
  <%= f.label :body, "Guestbook Entry:" %>
  <%= f.text_area :body %>
  <%= f.submit "Submit" %>
<% end %>

So it wants me to iterate through all the entries and display them on a welcome page that's located in view/pages/welcome.html.erb.

Up to this point I guess I've only been doing basic simple rails applications where the view corresponded with the controller, and followed the typical CRUD setup, where index would hold the @xxx = Xxxx.all and new/create would handle @xxx = Xxxx.new/create/build. I thought I could simply move the PageController's index action to create/new and do

def index
  @guestbook_entry = GuestbookEntry.all
  render "welcome"
end

To satisfy the test (it looks for render welcome in the index action)

This seems weird but again I admit, I'm a beginner.

Dipet
  • 323
  • 3
  • 14
  • 2
    what exactly you want to do? – Vrushali Pawar Jul 09 '15 at 08:48
  • Well I just had a moment of clarity. Or rather I should say, I realized I'm an idiot who needs to pay more attention. I was confused why the page wasn't called `index.html.erb` and for some reason the painfully obvious never occurred to me. I feel so dumb. – Dipet Jul 11 '15 at 22:21

1 Answers1

1

If you want to list all the guest book entries on your root page you would do something like:

class PagesController < ApplicationController
  def index
    @guestbook_entry = GuestbookEntry.new
    @guestbook_entries = GuestbookEntry.limit(10).all
    render "welcome"
  end
end

And in your view you would list them like:

<% if @guestbook_entries.any? %>
<div id="guestbook-entries">
  <p>Guestbook Entries:</p>
  <% @guestbook_entries.each do |entry| %>
  <ul>
    <li class="entry"><%= h(entry.body) %></li>
  </ul>
  <% end %>
</div>
<% end %>

The rest of you application is correct - you should be creating entries in GuestbookEntriesController#create. In many real life applications then the functionality of the standard new and edit actions can actually be a totally different controller.

max
  • 96,212
  • 14
  • 104
  • 165
  • `h(entry.body)` escapes any HTML in the entry body. See http://stackoverflow.com/questions/692921/rails-how-to-html-encode-escape-a-string-is-there-a-built-in for an explaination. – max Jul 09 '15 at 12:01
  • "In many real life applications then the functionality of the standard new and edit actions can actually be a totally different controller." I did not know this, I honestly thought the whole view file names had to match up with the controller actions. Thanks for this, added your explanation to my notes. Also thank you for the html escaping! – Dipet Jul 09 '15 at 16:23