落書きノート

ふと自分が気になった事を書いてます

Ruby on Rails 簡易掲示板

ウェブの情報を参考にしましたが、一から作ってみました。自分好みにしてあります。コマンドとかは省略させてください…。

posts_controller.rb

class PostsController < ApplicationController
  def index
    @posts = Post.all
  end

  def new
    @post = Post.new
  end

  def show
    
  end

  def create
    @post = Post.new(post_params)
    if @post.save
      redirect_to action: 'index'
    else
      render 'new'
    end
  end

  private

  def post_params
    params.require(:post).permit(:name, :text)
  end
end

post.rb

class Post < ActiveRecord::Base
  validates :name, :presence => true, :length => { :maximum => 10 }
  validates :text, :presence => true, :length => { :maximum => 100 }
end

index.html.erb

<h1>掲示板</h1>
<hr />
<% @posts.each do |post| %>
  <%= post.id %> : <%= post.name %> : <%= post.created_at %><br />
  <p><%= post.text %></p>
<% end %>

new.html.erb

<h1>掲示板</h1>
<%= form_for :post, url: posts_path do |f| %>
<% if @post.errors.any? %>
  <% @post.errors.full_messages.each do |err| %>
    <font color="red"><%= err %></font><br />
  <% end %>
<% end %>
<p>
  <%= f.label :name, "名前" %><br>
  <%= f.text_field :name %>
</p>
<p>
  <%= f.label :text, "内容" %><br>
  <%= f.text_area :text, cols: 60, rows: 8 %>
</p>
<p>
  <%= f.submit %>
</p>
<% end %>

routes.rb

Rails.application.routes.draw do
  # The priority is based upon order of creation: first created -> highest priority.
  # See how all your routes lay out with "rake routes".

  # You can have the root of your site routed with "root"
  root 'posts#index'

  # Example of regular route:
  #   get 'products/:id' => 'catalog#view'

  # Example of named route that can be invoked with purchase_url(id: product.id)
  #   get 'products/:id/purchase' => 'catalog#purchase', as: :purchase

  # Example resource route (maps HTTP verbs to controller actions automatically):
  resources :posts

  # Example resource route with options:
  #   resources :products do
  #     member do
  #       get 'short'
  #       post 'toggle'
  #     end
  #
  #     collection do
  #       get 'sold'
  #     end
  #   end

  # Example resource route with sub-resources:
  #   resources :products do
  #     resources :comments, :sales
  #     resource :seller
  #   end

  # Example resource route with more complex sub-resources:
  #   resources :products do
  #     resources :comments
  #     resources :sales do
  #       get 'recent', on: :collection
  #     end
  #   end

  # Example resource route with concerns:
  #   concern :toggleable do
  #     post 'toggle'
  #   end
  #   resources :posts, concerns: :toggleable
  #   resources :photos, concerns: :toggleable

  # Example resource route within a namespace:
  #   namespace :admin do
  #     # Directs /admin/products/* to Admin::ProductsController
  #     # (app/controllers/admin/products_controller.rb)
  #     resources :products
  #   end
end

YYYYMMDDHHMMSS_create_posts.rb

class CreatePosts < ActiveRecord::Migration
  def change
    create_table :posts do |t|
      t.string :name
      t.text :text

      t.timestamps null: false
    end
  end
end

20160209185223