#Model >article.rb belongs_to :user validates :title, presence: true, length: { maximum: 20 } validates :text, presence: true, length: { maximum: 100 } has_many :comments, dependent: :destroy >comment.rb belongs_to :article >user.rb has_many :articles devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable #View >articles/index.html.erb %= image_tag "1.jpg", width:200 %> %= link_to "新規記事作成", new_article_path %> % @articles.each do |article| %> h2>%= link_to article.title, article_path(article) %>/h2> p>%= article.text %>/p> %= link_to "編集", edit_article_path(article) %> %= link_to "消去", article_path(article), method: :delete, data: {confirm: "OK?"} %> % end %> p>%= link_to "TOP", welcome_index_path %>/p> >articles/show.html.erb h1>%= @article.title %>/h1> p><%= @article.text %>/p> hr /> %= render @article.comments %> %= render "comments/form" %> %= link_to "一覧に戻る", articles_path %> %= link_to "更新", edit_article_path(@article) %> >articles/_form.html.erb %= form_for @article do |f| %> % if @article.errors.any? %> %= @article.errors.count %>エラー div id="error_explanation" class="alert alert-danger"> ul> % @article.errors.full_messages.each do |message| %> li>%= message %>/li> % end %> /ul> /div> % end %> p>%= f.text_field :title %>/p> p>%= f.text_area :text %>/p> p>%= f.submit %>/p> % end %> >articles/new.html.erb %= render "form" %> >articles/edit.html.erb %= render "form" %> >comments/_form.html.erb %= form_for [@article, @article.comments.build] do |f| %> p>コメンター/p> %= f.text_field :commenter %> p>コメント/p> %= f.text_area :body %> p>%= f.submit %>/p> % end %> >_comment.html.erb %= comment.body %> %= comment.commenter %> %= link_to "削除", [comment.article, comment], method: :delete, data: {confirm: "OK?"} %> >layouts/application.html.erb blog hr /> % if user_signed_in?%> %= link_to "ログアウト", destroy_user_session_path, method: :delete %> % end %> p class="notice">%= notice %>/p> p class="alert">%= alert %>/p> %= yield %> #Controller >articles_controller.rb before_action :set_article, only: [:show, :edit, :update, :destroy] before_action :authenticate_user! def index #@articles = Article.all @articles = current_user.articles end def show set_article end def new @article = Article.new end def create #render plain: params[:article].inspect #raise params.inspect @article = Article.new(article_params) @article.user = current_user if @article.save redirect_to article_path(@article) else render "new" end end def edit end def update if @article.update(article_params) redirect_to @article else render "edit" end end def destroy @article.destroy redirect_to articles_path end def article_params params.require(:article).permit(:title, :text) end def set_article @article = Article.find params[:id] end >comments_controller.rb def create @article = Article.find params[:article_id] @article.comments.create(comment_params) redirect_to article_path(@article) end def destroy @article = Article.find params[:article_id] @comment = @article.comments.find params[:id] @comment.destroy redirect_to article_path(@article) end def comment_params params.require(:comment).permit(:commenter, :body) end routes.rb devise_for :users get 'welcome/index' resources :articles do resources :comments end root "welcome#index"