2013年7月15日月曜日

rails ネストサンプル


routes.rb
  resources :posts do
    resources :comments
  end

posts_controller.rb
class PostsController < ApplicationController
  # GET /posts
  # GET /posts.json
  def index
    @posts = Post.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @posts }
    end
  end

  # GET /posts/1
  # GET /posts/1.json
  def show
    @post = Post.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @post }
    end
  end

  # GET /posts/new
  # GET /posts/new.json
  def new
    @post = Post.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @post }
    end
  end

  # GET /posts/1/edit
  def edit
    @post = Post.find(params[:id])
  end

  # POST /posts
  # POST /posts.json
  def create
    @post = Post.new(params[:post])

    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
        format.json { render json: @post, status: :created, location: @post }
      else
        format.html { render action: "new" }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /posts/1
  # PUT /posts/1.json
  def update
    @post = Post.find(params[:id])

    respond_to do |format|
      if @post.update_attributes(params[:post])
        format.html { redirect_to @post, notice: 'Post was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /posts/1
  # DELETE /posts/1.json
  def destroy
    @post = Post.find(params[:id])
    @post.destroy

    respond_to do |format|
      format.html { redirect_to posts_url }
      format.json { head :no_content }
    end
  end
end

posts _form.html.erb
<%= form_for(@post) do |f| %>
  <% if @post.errors.any? %>
    

<%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:

    <% @post.errors.full_messages.each do |msg| %>
  • <%= msg %>
  • <% end %>
<% end %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :body %>
<%= f.text_area :body %>
<%= f.submit %>
<% end %>
posts edit.html.erb

Editing post

<%= render 'form' %> <%= link_to 'Show', @post %> | <%= link_to 'Back', posts_path %>
posts index.html.erb

Listing posts

<% @posts.each do |post| %> <% end %>
Title Body
<%= post.title %> <%= post.body %> <%= link_to 'Show', post %> <%= link_to 'Edit', edit_post_path(post) %> <%= link_to 'Edit', edit_post_url(post) %> <%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %>
<%= link_to 'New Post', new_post_path %>
posts show.html.erb

New post

<%= render 'form' %> <%= link_to 'Back', posts_path %>
<%= notice %>
Title: <%= @post.title %>
Body: <%= @post.body %>
<%= link_to 'Comments',post_comments_path(@post) %> <%= link_to 'Edit', edit_post_path(@post) %> | <%= link_to 'Back', posts_path %>
comments_controller.rb
class CommentsController < ApplicationController
  # GET /comments
  # GET /comments.json
  def index
    @post = Post.find(params[:post_id])

    @comments = @post.comments

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @comments }
    end
  end

  # GET /comments/1
  # GET /comments/1.json
  def show
    p params
    @post = Post.find(params[:post_id])
    @comment = Comment.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @comment }
    end
  end

  # GET /comments/new
  # GET /comments/new.json
  def new
    @post = Post.find(params[:post_id])
    @comment = @post.comments.new


    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @comment }
    end
  end

  # GET /comments/1/edit
  def edit
    @comment = Comment.find(params[:id])
  end

  # POST /comments
  # POST /comments.json
  def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.new(params[:comment])

    respond_to do |format|
      if @comment.save
        format.html { redirect_to [@post,@comment], notice: 'Comment was successfully created.' }
        format.json { render json: @comment, status: :created, location: @comment }
      else
        format.html { render action: "new" }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /comments/1
  # PUT /comments/1.json
  def update
    @post = Post.find(params[:post_id])
    @comment = Comment.find(params[:id])

    respond_to do |format|
      if @comment.update_attributes(params[:comment])
        format.html { redirect_to [@post,@comment], notice: 'Comment was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /comments/1
  # DELETE /comments/1.json
  def destroy
    @post = Post.find(params[:post_id])
    @comment = Comment.find(params[:id])
    @comment.destroy

    respond_to do |format|
      format.html { redirect_to post_comments_url(@post) }
      format.json { head :no_content }
    end
  end
end
comments _form.html.erb
<%= form_for([@post,@comment]) do |f| %>
  <% if @comment.errors.any? %>
    

<%= pluralize(@comment.errors.count, "error") %> prohibited this comment from being saved:

    <% @comment.errors.full_messages.each do |msg| %>
  • <%= msg %>
  • <% end %>
<% end %>
<%#= f.label :post %>
<%#= f.text_field :post %>
<%= f.label :body %>
<%= f.text_area :body %>
<%= f.submit %>
<% end %>
comments edit.html.erb

Editing comment

<%= render 'form' %> <%= link_to 'Show', [@post,@comment] %> | <%= link_to 'Back', post_comment_path %>
comments index.html.erb

Listing comments

<% @comments.each do |comment| %> <% end %>
Post Body
<%= comment.post.title %> <%= comment.body %> <%= link_to 'Show', post_comment_path(@post,comment) %> <%= link_to 'Edit', edit_post_comment_path(@post,comment) %> <%= link_to 'Destroy', post_comment_path(@post, comment), method: :delete, data: { confirm: 'Are you sure?' } %>
<%= link_to 'back' ,post_path(@post) %> <%= link_to 'New Comment', new_post_comment_path %>
comments new.html.erb

New comment

<%= render 'form' %> <%= link_to 'Back', post_comments_path(@post.id) %>
<%= notice %>
Post: <%= @comment.post %>
Body: <%= @comment.body %>
<%= link_to 'Edit', edit_post_comment_path %> | <%= link_to 'Back', post_comments_path %>
models posts.rb
class Post < ActiveRecord::Base
  attr_accessible :body, :title
  has_many :comments

end
models post.rb
class Comment < ActiveRecord::Base
  belongs_to :post
  attr_accessible :body
end

0 件のコメント:

コメントを投稿