diff options
Diffstat (limited to 'guides/code/getting_started/app')
24 files changed, 237 insertions, 0 deletions
| diff --git a/guides/code/getting_started/app/assets/images/rails.png b/guides/code/getting_started/app/assets/images/rails.pngBinary files differ new file mode 100644 index 0000000000..d5edc04e65 --- /dev/null +++ b/guides/code/getting_started/app/assets/images/rails.png diff --git a/guides/code/getting_started/app/assets/javascripts/application.js b/guides/code/getting_started/app/assets/javascripts/application.js new file mode 100644 index 0000000000..9097d830e2 --- /dev/null +++ b/guides/code/getting_started/app/assets/javascripts/application.js @@ -0,0 +1,15 @@ +// This is a manifest file that'll be compiled into application.js, which will include all the files +// listed below. +// +// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts, +// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path. +// +// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the +// the compiled file. +// +// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD +// GO AFTER THE REQUIRES BELOW. +// +//= require jquery +//= require jquery_ujs +//= require_tree . diff --git a/guides/code/getting_started/app/assets/stylesheets/application.css b/guides/code/getting_started/app/assets/stylesheets/application.css new file mode 100644 index 0000000000..3b5cc6648e --- /dev/null +++ b/guides/code/getting_started/app/assets/stylesheets/application.css @@ -0,0 +1,13 @@ +/* + * This is a manifest file that'll be compiled into application.css, which will include all the files + * listed below. + * + * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets, + * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path. + * + * You're free to add application-wide styles to this file and they'll appear at the top of the + * compiled file, but it's generally better to create a new file per style scope. + * + *= require_self + *= require_tree . +*/ diff --git a/guides/code/getting_started/app/controllers/application_controller.rb b/guides/code/getting_started/app/controllers/application_controller.rb new file mode 100644 index 0000000000..e8065d9505 --- /dev/null +++ b/guides/code/getting_started/app/controllers/application_controller.rb @@ -0,0 +1,3 @@ +class ApplicationController < ActionController::Base +  protect_from_forgery +end diff --git a/guides/code/getting_started/app/controllers/comments_controller.rb b/guides/code/getting_started/app/controllers/comments_controller.rb new file mode 100644 index 0000000000..cf3d1be42e --- /dev/null +++ b/guides/code/getting_started/app/controllers/comments_controller.rb @@ -0,0 +1,17 @@ +class CommentsController < ApplicationController +	http_basic_authenticate_with :name => "dhh", :password => "secret", :only => :destroy + +  def create +    @post = Post.find(params[:post_id]) +    @comment = @post.comments.create(params[:comment]) +    redirect_to post_path(@post) +  end + +  def destroy +    @post = Post.find(params[:post_id]) +    @comment = @post.comments.find(params[:id]) +    @comment.destroy +    redirect_to post_path(@post) +  end + +end diff --git a/guides/code/getting_started/app/controllers/home_controller.rb b/guides/code/getting_started/app/controllers/home_controller.rb new file mode 100644 index 0000000000..309b70441e --- /dev/null +++ b/guides/code/getting_started/app/controllers/home_controller.rb @@ -0,0 +1,5 @@ +class WelcomeController < ApplicationController +  def index +  end + +end diff --git a/guides/code/getting_started/app/controllers/posts_controller.rb b/guides/code/getting_started/app/controllers/posts_controller.rb new file mode 100644 index 0000000000..a8ac9aba5a --- /dev/null +++ b/guides/code/getting_started/app/controllers/posts_controller.rb @@ -0,0 +1,47 @@ +class PostsController < ApplicationController + +  http_basic_authenticate_with :name => "dhh", :password => "secret", :except => [:index, :show] + +  def index +    @posts = Post.all +  end + +  def show +    @post = Post.find(params[:id]) +  end + +  def new +    @post = Post.new +  end + +  def create +    @post = Post.new(params[:post]) + +    if @post.save +      redirect_to :action => :show, :id => @post.id +    else +      render 'new' +    end +  end + +  def edit +    @post = Post.find(params[:id]) +  end + +  def update +    @post = Post.find(params[:id]) + +    if @post.update_attributes(params[:post]) +      redirect_to :action => :show, :id => @post.id +    else +      render 'edit' +    end +  end + +  def destroy +    @post = Post.find(params[:id]) +    @post.destroy + +    redirect_to :action => :index +  end +end diff --git a/guides/code/getting_started/app/helpers/application_helper.rb b/guides/code/getting_started/app/helpers/application_helper.rb new file mode 100644 index 0000000000..de6be7945c --- /dev/null +++ b/guides/code/getting_started/app/helpers/application_helper.rb @@ -0,0 +1,2 @@ +module ApplicationHelper +end diff --git a/guides/code/getting_started/app/helpers/comments_helper.rb b/guides/code/getting_started/app/helpers/comments_helper.rb new file mode 100644 index 0000000000..0ec9ca5f2d --- /dev/null +++ b/guides/code/getting_started/app/helpers/comments_helper.rb @@ -0,0 +1,2 @@ +module CommentsHelper +end diff --git a/guides/code/getting_started/app/helpers/posts_helper.rb b/guides/code/getting_started/app/helpers/posts_helper.rb new file mode 100644 index 0000000000..a7b8cec898 --- /dev/null +++ b/guides/code/getting_started/app/helpers/posts_helper.rb @@ -0,0 +1,2 @@ +module PostsHelper +end diff --git a/guides/code/getting_started/app/helpers/welcome_helper.rb b/guides/code/getting_started/app/helpers/welcome_helper.rb new file mode 100644 index 0000000000..eeead45fc9 --- /dev/null +++ b/guides/code/getting_started/app/helpers/welcome_helper.rb @@ -0,0 +1,2 @@ +module WelcomeHelper +end diff --git a/guides/code/getting_started/app/mailers/.gitkeep b/guides/code/getting_started/app/mailers/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/guides/code/getting_started/app/mailers/.gitkeep diff --git a/guides/code/getting_started/app/models/.gitkeep b/guides/code/getting_started/app/models/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/guides/code/getting_started/app/models/.gitkeep diff --git a/guides/code/getting_started/app/models/comment.rb b/guides/code/getting_started/app/models/comment.rb new file mode 100644 index 0000000000..4e76c5b5b0 --- /dev/null +++ b/guides/code/getting_started/app/models/comment.rb @@ -0,0 +1,3 @@ +class Comment < ActiveRecord::Base +  belongs_to :post +end diff --git a/guides/code/getting_started/app/models/post.rb b/guides/code/getting_started/app/models/post.rb new file mode 100644 index 0000000000..21387340b0 --- /dev/null +++ b/guides/code/getting_started/app/models/post.rb @@ -0,0 +1,6 @@ +class Post < ActiveRecord::Base +  validates :title, :presence => true, +                    :length => { :minimum => 5 } + +  has_many :comments, :dependent => :destroy +end diff --git a/guides/code/getting_started/app/views/comments/_comment.html.erb b/guides/code/getting_started/app/views/comments/_comment.html.erb new file mode 100644 index 0000000000..0cebe0bd96 --- /dev/null +++ b/guides/code/getting_started/app/views/comments/_comment.html.erb @@ -0,0 +1,15 @@ +<p> +  <strong>Commenter:</strong> +  <%= comment.commenter %> +</p> + +<p> +  <strong>Comment:</strong> +  <%= comment.body %> +</p> + +<p> +  <%= link_to 'Destroy Comment', [comment.post, comment], +               :confirm => 'Are you sure?', +               :method => :delete %> +</p> diff --git a/guides/code/getting_started/app/views/comments/_form.html.erb b/guides/code/getting_started/app/views/comments/_form.html.erb new file mode 100644 index 0000000000..00cb3a08f0 --- /dev/null +++ b/guides/code/getting_started/app/views/comments/_form.html.erb @@ -0,0 +1,13 @@ +<%= form_for([@post, @post.comments.build]) do |f| %> +  <p> +    <%= f.label :commenter %><br /> +    <%= f.text_field :commenter %> +  </p> +  <p> +    <%= f.label :body %><br /> +    <%= f.text_area :body %> +  </p> +  <p> +    <%= f.submit %> +  </p> +<% end %> diff --git a/guides/code/getting_started/app/views/layouts/application.html.erb b/guides/code/getting_started/app/views/layouts/application.html.erb new file mode 100644 index 0000000000..6578a41da2 --- /dev/null +++ b/guides/code/getting_started/app/views/layouts/application.html.erb @@ -0,0 +1,14 @@ +<!DOCTYPE html> +<html> +<head> +  <title>Blog</title> +  <%= stylesheet_link_tag    "application", :media => "all" %> +  <%= javascript_include_tag "application" %> +  <%= csrf_meta_tags %> +</head> +<body> + +<%= yield %> + +</body> +</html> diff --git a/guides/code/getting_started/app/views/posts/_form.html.erb b/guides/code/getting_started/app/views/posts/_form.html.erb new file mode 100644 index 0000000000..f22139938c --- /dev/null +++ b/guides/code/getting_started/app/views/posts/_form.html.erb @@ -0,0 +1,25 @@ +<%= form_for @post do |f| %> +  <% if @post.errors.any? %> +    <div id="errorExplanation"> +      <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2> +      <ul> +        <% @post.errors.full_messages.each do |msg| %> +          <li><%= msg %></li> +        <% end %> +      </ul> +    </div> +  <% end %> +  <p> +    <%= f.label :title %><br /> +    <%= f.text_field :title %> +  </p> + +  <p> +    <%= f.label :text %><br /> +    <%= f.text_area :text %> +  </p> + +  <p> +    <%= f.submit %> +  </p> +<% end %> diff --git a/guides/code/getting_started/app/views/posts/edit.html.erb b/guides/code/getting_started/app/views/posts/edit.html.erb new file mode 100644 index 0000000000..911a48569d --- /dev/null +++ b/guides/code/getting_started/app/views/posts/edit.html.erb @@ -0,0 +1,5 @@ +<h1>Editing post</h1> + +<%= render 'form' %> + +<%= link_to 'Back', :action => :index %> diff --git a/guides/code/getting_started/app/views/posts/index.html.erb b/guides/code/getting_started/app/views/posts/index.html.erb new file mode 100644 index 0000000000..7b72720d50 --- /dev/null +++ b/guides/code/getting_started/app/views/posts/index.html.erb @@ -0,0 +1,23 @@ +<h1>Listing posts</h1> + +<%= link_to 'New post', :action => :new %> + +<table> +  <tr> +    <th>Title</th> +    <th>Text</th> +    <th></th> +    <th></th> +    <th></th> +  </tr> + +<% @posts.each do |post| %> +  <tr> +    <td><%= post.title %></td> +    <td><%= post.text %></td> +    <td><%= link_to 'Show', :action => :show, :id => post.id %> +    <td><%= link_to 'Edit', :action => :edit, :id => post.id %> +    <td><%= link_to 'Destroy', { :action => :destroy, :id => post.id }, :method => :delete, :confirm => 'Are you sure?' %> +  </tr> +<% end %> +</table> diff --git a/guides/code/getting_started/app/views/posts/new.html.erb b/guides/code/getting_started/app/views/posts/new.html.erb new file mode 100644 index 0000000000..ce9523a721 --- /dev/null +++ b/guides/code/getting_started/app/views/posts/new.html.erb @@ -0,0 +1,5 @@ +<h1>New post</h1> + +<%= render 'form' %> + +<%= link_to 'Back', :action => :index %> diff --git a/guides/code/getting_started/app/views/posts/show.html.erb b/guides/code/getting_started/app/views/posts/show.html.erb new file mode 100644 index 0000000000..65809033ed --- /dev/null +++ b/guides/code/getting_started/app/views/posts/show.html.erb @@ -0,0 +1,18 @@ +<p> +  <strong>Title:</strong> +  <%= @post.title %> +</p> + +<p> +  <strong>Text:</strong> +  <%= @post.text %> +</p> + +<h2>Comments</h2> +<%= render @post.comments %> + +<h2>Add a comment:</h2> +<%= render "comments/form" %> + +<%= link_to 'Edit Post', edit_post_path(@post) %> | +<%= link_to 'Back to Posts', posts_path %> diff --git a/guides/code/getting_started/app/views/welcome/index.html.erb b/guides/code/getting_started/app/views/welcome/index.html.erb new file mode 100644 index 0000000000..e04680ea7e --- /dev/null +++ b/guides/code/getting_started/app/views/welcome/index.html.erb @@ -0,0 +1,2 @@ +<h1>Hello, Rails!</h1> +<%= link_to "My Blog", :controller => "posts" %> | 
