Rails for Zombies 1 3 The View - mikesabat/LR_HF GitHub Wiki

The View

ERB stands for Embedded Ruby. These pages allow you to put Ruby into HTML so the page can process logic. There are two new tags with erb.

  • <% ... &> -- the less than percent tag will evaluate the contents as Ruby.
  • <%= ... %> -- the less than percent equals tag will evaluate the contents as Ruby and then print the result to the page.

Common elements of the page should go into a file at /app/views/layouts/application.html.erb. By doing this, you won't need to put headers and common elements into every page (DRY). In the layout, when it calls for the individual page to run, use <%= yield %>

In the layout, <%= stylesheet_link_tag :all %> will tell rails to use all of the stylesheets with are located in the /public/stylesheets folder. For any address entered into your domain. The first thing rails does is check the public directory to see if the page exists.

Linking

<%= link_to link.text, link.path %> <%= link_to tweet.zombie.name, zombie_path(tweet.zombie) %>

OR We can make this easier.

<%= link_to link.text, object.to.show %> <%= link_to tweet.zombie.name, tweet.zombie %>

link_to is a helper method.

Resources

  1. Look in the source - git clone http://github.com/rails/rails.git search 'def link_to' to look up the link_to helper method.

  2. api.rubyonrails.org

  3. apidoc.com/rails

  4. railsapi.com - rails searchable API doc. Also downloadable.

Index Page

<% tweets = Tweet.all %>

<% tweets.each do |tweet| %>

<%= link_to tweet.status, tweet %> <%= link_to tweet.zombie.name, tweet.zombie %> <% end %>

Edit and Delete Paths

<%= link_to "Edit", edit_tweet_path(tweet) %>

<%= link_to "Delete", tweet :method => :delete %>

Paths

List all tweets tweets_path /tweets

New tweet new_tweet_path /tweets/new

Show a tweet tweet /tweets/1

Edit a tweet edit_tweet_path /tweets/1/edit

Delete a tweet tweet, :method => :delete /tweets/1

⚠️ **GitHub.com Fallback** ⚠️