2014年3月23日日曜日

新しいPC

消費税アップ直前、XPサポート切れ直前に新しいPCを購入。
ノートのCPUを使っているだけあって静です。スピードも特に問題無し。
新しいPCを電話の台に使ってみました




2014年3月2日日曜日

rails ネストしたモデルのリスト編集とモデルのリスト編集

結果的にはネストしたモデルのリスト編集の方がコードが少なく簡単にできました。
リスト編集はもっと簡単に出来ないのかな???

いつものようにコードが正しく張り付かないのでソースはここに置いています
ネストしたモデル側にリスト編集(bookにたいするcommentのリスト作成、編集)

モデルのリスト編集(bookのタイトルの作成、編集)

編集側

どちらもポイントはfield_forです。


  resources :comments

  resources :books do
    get 'edit_many',:on => :collection
    get 'new_many', :on => :collection
    post 'update_many' ,:on => :collection
    post 'create_many'  ,:on => :collection
    get 'destroy_comment', :on => :member
  end
class Book < ActiveRecord::Base
  has_many :comments
  accepts_nested_attributes_for :comments ,:allow_destroy => true

  validates :title,:presence => true
end
class Comment < ActiveRecord::Base
  belongs_to :book
  validates :content,:presence => true
end

class BooksController < ApplicationController
  before_action :set_book, only: [:show, :edit, :update, :destroy,:destroy_comment]

  # GET /books
  # GET /books.json
  def index
    @books = Book.all
  end

  # GET /books/1
  # GET /books/1.json
  def show
  end

  # GET /books/new
  def new
    @book = Book.new
  end
  def new_many
    @book =[]
    @book << Book.new
    @next_action = :create_many
  end

  # GET /books/1/edit
  def edit
  end
  def edit_many
    @book = []
    @book = Book.all
    @next_action = :update_many
  end

  # POST /books
  # POST /books.json
  def create
    @book = Book.new(book_params)
    if params[:commit] == "追加" then
      @book.comments.build

      render :action => :new
      return
    end

    respond_to do |format|
      if @book.save
        format.html { redirect_to @book, notice: 'Book was successfully created.' }
        format.json { render action: 'show', status: :created, location: @book }
      else
        format.html { render action: 'new' }
        format.json { render json: @book.errors, status: :unprocessable_entity }
      end
    end
  end
  def create_many
    @book = []
    params[:book].each{|book|
      @book << Book.new(book[1])
    }
    @book.each{|book|book.valid?}
    if params[:commit] =="追加" then
      @book << Book.new
      @next_action = :create_many
      render new_many_books_path
      return
    end
    @isError = false
    @book.each{|book|@isError = true if book.errors.any?}
    if @isError == false then
      @book.each{|book|book.save}
      redirect_to :action => :index
    else
      render new_many_books_path
    end
  end

  # PATCH/PUT /books/1
  # PATCH/PUT /books/1.json
  def update
    if params[:commit] == "追加" then
      @book.update_attributes(book_params)
      @book.comments.new
      render :action => :edit
      return
    end
    respond_to do |format|
      if @book.update(book_params)
        format.html { redirect_to @book, notice: 'Book was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @book.errors, status: :unprocessable_entity }
      end
    end
  end
  def update_many
    @book =[]
    params[:book].each{|book|
      obj=""
      if book[1]["id"].present? then
        obj = Book.find(book[1][:id])
        obj.valid?(book[1])
      else
        obj= Book.new(book[1])
        obj.valid?
      end
      @book << obj
    }
    @isError = false
    @book.each{|book| @isError = true if book.errors.any?}
    if params[:commit] == "追加" then
      obj = Book.new()
      @book << obj
      @next_action = :update_many
      render edit_many_books_path
      return
    end
    if @isError == false then
      @book.each{|book|book.save}
      redirect_to books_path
    else
      render edit_may_books_path
    end
  end

  # DELETE /books/1
  # DELETE /books/1.json
  def destroy
    @book.destroy
    respond_to do |format|
      format.html { redirect_to books_url }
      format.json { head :no_content }
    end
  end
  def destroy_comment
    comment = Comment.find(params[:comment_id].to_i)
    comment.destroy
    respond_to do |format|
      format.html{ redirect_to edit_book_path(params[:id])}
    end
  end



  private
    # Use callbacks to share common setup or constraints between actions.
    def set_book
      @book = Book.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def book_params
      params.require(:book).permit(:title,:id,:comments_attributes =>[:content,:id])
    end
end
<%= form_for(@book) do |f| %>
  <% if @book.errors.any? %>
    

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

    <% @book.errors.full_messages.each do |msg| %>
  • <%= msg %>
  • <% end %>
<% end %>
<%= f.label :title %>
<%= f.text_field :title %>

<%= f.fields_for :comments do |a| %> <%= a.text_field :content %> <%= link_to "削除",destroy_comment_book_path(@book.id,:comment_id => @book.comments[a.index].id) if @book.comments[a.index].id.present?%> <% end %>
<%= f.submit %> <%= f.submit "追加" %>
<% end %>
<%= form_tag :action => @next_action do  %>
    <% if @isError == true then %>
        

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

    <% @book.each{|book| %> <% book.errors.full_messages.each do |msg| %>
  • <%= msg %>
  • <% end %> <% } %>
<% end %> <% @book.each_with_index{|book,i| %> <%#= book.index %> <%#= i %> <%= fields_for("book[#{i}]",book) {|f| %>
<%= f.label :title %>
<%= f.text_field :title %> <%= f.hidden_field :id %>
<% } %> <% } %> <%= submit_tag "送信" %> <%= submit_tag "追加" %> <% end %>