Skip to content
This repository
  • 5 commits
  • 1 file changed
  • 0 comments
  • 2 contributors

Showing 1 changed file with 14 additions and 16 deletions. Show diff stats Hide diff stats

  1. +14 16 source/projects/blogger.markdown
30 source/projects/blogger.markdown
Source Rendered
@@ -72,7 +72,7 @@ It generally takes about 15 seconds. When you see seven lines like this:
72 72
73 73 ```plain
74 74 => Booting WEBrick
75   -=> Rails 3.1.3 application starting in development on http://0.0.0.0:3000
  75 +=> Rails 3.2.2 application starting in development on http://0.0.0.0:3000
76 76 => Call with -d to detach
77 77 => Ctrl-C to shutdown server
78 78 [2012-01-07 11:16:52] INFO WEBrick 1.3.1
@@ -302,6 +302,12 @@ The router tried to call the `index` action, but the articles controller doesn't
302 302
303 303 What is that "at" sign doing on the front of `@articles`? That marks this variable as an "instance level variable". We want the list of articles to be accessible from both the controller and the view that we're about to create. In order for it to be visible in both places it has to be an instance variable. If we had just named it `articles`, that local variable would only be available within the `index` method of the controller.
304 304
  305 +A normal Ruby instance variable is available to all methods within an instance.
  306 +
  307 +In Rails' controllers, there's a *hack* which allows instance variables to be automatically transferred from the controller to the object which renders the view template. So any data we want available in the view template should be promoted to an instance variable by adding a `@` to the beginning.
  308 +
  309 +There are ways to accomplish the same goals without instance variables, but they're not widely used. Check out the [Decent Exposure](https://github.com/voxdolo/decent_exposure) gem to learn more.
  310 +
305 311 ### Creating the Template
306 312
307 313 Now refresh your browser. The error message changed, but you've still got an error, right?
@@ -443,14 +449,6 @@ Within that hash we can find the `:id` from the URL by accessing the key `params
443 449 @article = Article.find(params[:id])
444 450 ```
445 451
446   -#### What is `@article`
447   -
448   -The last line we wrote created an instance variable named `@article`. A normal Ruby instance variable is available to all methods within an instance.
449   -
450   -In Rails' controllers, there's a *hack* which allows instance variables to be automatically transferred from the controller to the object which renders the view template. So any data we want available in the view template should be promoted to an instance variable by adding a `@` to the beginning.
451   -
452   -There are ways to accomplish the same goals without instance variables, but they're not widely used. Check out the [Decent Exposure](https://github.com/voxdolo/decent_exposure) gem to learn more.
453   -
454 452 #### Back to the Template
455 453
456 454 Refresh your browser and we still have the "Template is Missing" error. Create the file `app/views/articles/show.html.erb` and add this code:
@@ -879,7 +877,7 @@ Now try editing and saving some of your articles.
879 877
880 878 Our operations are working, but it would be nice if we gave the user some kind of status message about what took place. When we create an article the message might say "Article 'the-article-title' was created", or "Article 'the-article-title' was removed" for the remove action. We can accomplish this with the `flash`.
881 879
882   -The controller provides you two methods to interact with the `flash`. Calling `flash[:notice]` will fetch a value from the flash, or `flash[:notice] = "Your Message"` will store the string in the `flash`. So it looks and acts just like a hash.
  880 +The controller provides you with accessors to interact with the `flash`. Calling `flash.notice` will fetch a value, and `flash.notice = "Your Message"` will store the string in the `flash`.
883 881
884 882 #### Flash for Update
885 883
@@ -901,7 +899,7 @@ We can add a flash message by inserting one line:
901 899 @article = Article.find(params[:id])
902 900 @article.update_attributes(params[:article])
903 901
904   - flash[:message] = "Article '#{@article.title}' Updated!"
  902 + flash.notice = "Article '#{@article.title}' Updated!"
905 903
906 904 redirect_to article_path(@article)
907 905 end
@@ -941,10 +939,10 @@ Looking at the default layout, you'll see this:
941 939 The `yield` is where the view template content will be injected. Just *above* that yield, let's display the flash by adding this:
942 940
943 941 ```erb
944   -<p class="flash"><%= flash[:message] %></p>
  942 +<p class="flash"><%= flash.notice %></p>
945 943 ```
946 944
947   -This outputs the value stored in the `flash` object with the key `:message`.
  945 +This outputs the value stored in the `flash` object in the attribute `:notice`.
948 946
949 947 #### More Flash Testing
950 948
@@ -1933,7 +1931,7 @@ In this layout we'll put the view code that we want to render for every view tem
1933 1931 = csrf_meta_tags
1934 1932 %body
1935 1933 %p.flash
1936   - = flash[:message]
  1934 + = flash.notice
1937 1935 = yield
1938 1936 ```
1939 1937
@@ -2135,7 +2133,7 @@ Let's open `app/views/layouts/application.html.haml` and add a little footer so
2135 2133 ```ruby
2136 2134 %body
2137 2135 %p.flash
2138   - = flash[:message]
  2136 + = flash.notice
2139 2137 #container
2140 2138 #content
2141 2139 = yield
@@ -2163,7 +2161,7 @@ class AuthorSessionsController < ApplicationController
2163 2161 if @author == login(params[:username], params[:password])
2164 2162 redirect_back_or_to(articles_path, :message => 'Logged in successfully.')
2165 2163 else
2166   - flash.now[:alert] = "Login failed."
  2164 + flash.now.alert = "Login failed."
2167 2165 render :action => :new
2168 2166 end
2169 2167 end

No commit comments for this range

Something went wrong with that request. Please try again.