diff options
author | Kazuki Yamaguchi <k@rhe.jp> | 2015-12-26 21:42:49 +0900 |
---|---|---|
committer | Kazuki Yamaguchi <k@rhe.jp> | 2015-12-26 21:42:49 +0900 |
commit | aa521c6d4105374a5c7837330a98eba58149c675 (patch) | |
tree | 67e5c3538d07dc7d66ef75de5ebc8135b24905db | |
parent | 24c3623eaa1b82bcf97cb4d5a4f131d9cc2a856f (diff) | |
download | poe-aa521c6d4105374a5c7837330a98eba58149c675.tar.gz |
create controller snippets
-rw-r--r-- | app/assets/javascripts/snippets.js | 2 | ||||
-rw-r--r-- | app/assets/stylesheets/scaffold.css | 84 | ||||
-rw-r--r-- | app/assets/stylesheets/snippets.css | 4 | ||||
-rw-r--r-- | app/controllers/snippets_controller.rb | 62 | ||||
-rw-r--r-- | app/helpers/snippets_helper.rb | 2 | ||||
-rw-r--r-- | app/views/snippets/_form.html.erb | 27 | ||||
-rw-r--r-- | app/views/snippets/edit.html.erb | 6 | ||||
-rw-r--r-- | app/views/snippets/index.html.erb | 29 | ||||
-rw-r--r-- | app/views/snippets/new.html.erb | 5 | ||||
-rw-r--r-- | app/views/snippets/show.html.erb | 14 | ||||
-rw-r--r-- | config/routes.rb | 9 |
11 files changed, 244 insertions, 0 deletions
diff --git a/app/assets/javascripts/snippets.js b/app/assets/javascripts/snippets.js new file mode 100644 index 0000000..dee720f --- /dev/null +++ b/app/assets/javascripts/snippets.js @@ -0,0 +1,2 @@ +// Place all the behaviors and hooks related to the matching controller here. +// All this logic will automatically be available in application.js. diff --git a/app/assets/stylesheets/scaffold.css b/app/assets/stylesheets/scaffold.css new file mode 100644 index 0000000..79f8b7f --- /dev/null +++ b/app/assets/stylesheets/scaffold.css @@ -0,0 +1,84 @@ +body { + background-color: #fff; + color: #333; +} + +body, p, ol, ul, td { + font-family: verdana, arial, helvetica, sans-serif; + font-size: 13px; + line-height: 18px; + margin: 33px; +} + +pre { + background-color: #eee; + padding: 10px; + font-size: 11px; +} + +a { + color: #000; +} + +a:visited { + color: #666; +} + +a:hover { + color: #fff; + background-color: #000; +} + +th { + padding-bottom: 5px; +} + +td { + padding-bottom: 7px; + padding-left: 5px; + padding-right: 5px; +} + +div.field, +div.actions { + margin-bottom: 10px; +} + +#notice { + color: green; +} + +.field_with_errors { + padding: 2px; + background-color: red; + display: table; +} + +#error_explanation { + width: 450px; + border: 2px solid red; + padding: 7px; + padding-bottom: 0; + margin-bottom: 20px; + background-color: #f0f0f0; +} + +#error_explanation h2 { + text-align: left; + font-weight: bold; + padding: 5px 5px 5px 15px; + font-size: 12px; + margin: -7px; + margin-bottom: 0; + background-color: #c00; + color: #fff; +} + +#error_explanation ul li { + font-size: 12px; + list-style: square; +} + +label { + display: block; +} diff --git a/app/assets/stylesheets/snippets.css b/app/assets/stylesheets/snippets.css new file mode 100644 index 0000000..afad32d --- /dev/null +++ b/app/assets/stylesheets/snippets.css @@ -0,0 +1,4 @@ +/* + Place all the styles related to the matching controller here. + They will automatically be included in application.css. +*/ diff --git a/app/controllers/snippets_controller.rb b/app/controllers/snippets_controller.rb new file mode 100644 index 0000000..7d6f1b3 --- /dev/null +++ b/app/controllers/snippets_controller.rb @@ -0,0 +1,62 @@ +class SnippetsController < ApplicationController + before_action :set_snippet, only: [:show] + + # GET /snippets + # TODO + # def index + # @snippets = Snippet.all + # end + + # GET /1234 + def show + end + + # GET / + def new + @snippet = Snippet.new + end + + # GET /snippets/1/edit + # TODO + # def edit + # end + + # POST /create + def create + @snippet = Snippet.new(snippet_params) + + if @snippet.save + redirect_to @snippet, notice: "Snippet was successfully created." + else + render :new + end + end + + # PATCH/PUT /snippets/1 + # TODO + # def update + # if @snippet.update(snippet_params) + # redirect_to @snippet, notice: "Snippet was successfully updated." + # else + # render :edit + # end + # end + + # DELETE /snippets/1 + # TODO + # def destroy + # @snippet.destroy + # redirect_to snippets_url, notice: "Snippet was successfully destroyed." + # end + + private + # Use callbacks to share common setup or constraints between actions. + def set_snippet + @snippet = Snippet.find(params[:id]) + end + + # Only allow a trusted parameter "white list" through. + def snippet_params + params.require(:snippet).permit(:title, :code) + end +end diff --git a/app/helpers/snippets_helper.rb b/app/helpers/snippets_helper.rb new file mode 100644 index 0000000..236b6c8 --- /dev/null +++ b/app/helpers/snippets_helper.rb @@ -0,0 +1,2 @@ +module SnippetsHelper +end diff --git a/app/views/snippets/_form.html.erb b/app/views/snippets/_form.html.erb new file mode 100644 index 0000000..1c37b00 --- /dev/null +++ b/app/views/snippets/_form.html.erb @@ -0,0 +1,27 @@ +<%= form_for(snippet) do |f| %> + <% if snippet.errors.any? %> + <div id="error_explanation"> + <h2><%= pluralize(snippet.errors.count, "error") %> prohibited this snippet from being saved:</h2> + + <ul> + <% snippet.errors.full_messages.each do |message| %> + <li><%= message %></li> + <% end %> + </ul> + </div> + <% end %> + + <div class="field"> + <%= f.label :title %> + <%= f.text_field :title %> + </div> + + <div class="field"> + <%= f.label :code %> + <%= f.text_area :code %> + </div> + + <div class="actions"> + <%= f.submit %> + </div> +<% end %> diff --git a/app/views/snippets/edit.html.erb b/app/views/snippets/edit.html.erb new file mode 100644 index 0000000..c27040f --- /dev/null +++ b/app/views/snippets/edit.html.erb @@ -0,0 +1,6 @@ +<h1>Editing Snippet</h1> + +<%= render 'form', snippet: @snippet %> + +<%= link_to 'Show', @snippet %> | +<%= link_to 'Back', snippets_path %> diff --git a/app/views/snippets/index.html.erb b/app/views/snippets/index.html.erb new file mode 100644 index 0000000..601ba13 --- /dev/null +++ b/app/views/snippets/index.html.erb @@ -0,0 +1,29 @@ +<p id="notice"><%= notice %></p> + +<h1>Snippets</h1> + +<table> + <thead> + <tr> + <th>Title</th> + <th>Code</th> + <th colspan="3"></th> + </tr> + </thead> + + <tbody> + <% @snippets.each do |snippet| %> + <tr> + <td><%= snippet.title %></td> + <td><%= snippet.code %></td> + <td><%= link_to 'Show', snippet %></td> + <td><%= link_to 'Edit', edit_snippet_path(snippet) %></td> + <td><%= link_to 'Destroy', snippet, method: :delete, data: { confirm: 'Are you sure?' } %></td> + </tr> + <% end %> + </tbody> +</table> + +<br> + +<%= link_to 'New Snippet', new_snippet_path %> diff --git a/app/views/snippets/new.html.erb b/app/views/snippets/new.html.erb new file mode 100644 index 0000000..0a9ebaf --- /dev/null +++ b/app/views/snippets/new.html.erb @@ -0,0 +1,5 @@ +<h1>New Snippet</h1> + +<%= render 'form', snippet: @snippet %> + +<%= link_to 'Back', snippets_path %> diff --git a/app/views/snippets/show.html.erb b/app/views/snippets/show.html.erb new file mode 100644 index 0000000..9e59d7a --- /dev/null +++ b/app/views/snippets/show.html.erb @@ -0,0 +1,14 @@ +<p id="notice"><%= notice %></p> + +<p> + <strong>Title:</strong> + <%= @snippet.title %> +</p> + +<p> + <strong>Code:</strong> + <%= @snippet.code %> +</p> + +<%= link_to 'Edit', edit_snippet_path(@snippet) %> | +<%= link_to 'Back', snippets_path %> diff --git a/config/routes.rb b/config/routes.rb index 927efe4..19d6a03 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,15 @@ Rails.application.routes.draw do + resources :snippets # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html # Serve websocket cable requests in-process # mount ActionCable.server => "/cable" + + root to: "snippets#new" + + post "/create" => "snippets#create" + put "/update" => "snippets#update" + get "/:id" => "snippets#show", constraints: { id: /[1-9][0-9]*/ } + + match "*any" => "application#not_found", via: :all end |