這邊要做一個 Form Object,使其送一次的 submit ,而達到兩個 model 的更新
在 girb 裡,會有 name, like, email, level_cd,這樣的欄位需要維護
我們新建一個 model 名稱訂為 xfile ,要維護的欄位為 character
最終的目的就是想要 name, like, email, level_cd, character 這5個欄位一起維護
首先來產生 model
以及讓這兩個 model 有所關聯
之前已經先建了 girb 這個 model
現在再產生一個 model 為 xfile,並開一個欄位 girb_id 及 character
藉由欄位 girb_id,這個 model xfile 能和 model girb 有所關連
rails g model xfile girb_id:integer character:string
rake db:migrate
annotate
app/models/girb.rb
# == Schema Information
#
# Table name: girbs
#
# id :integer not null, primary key
# name :string(255)
# created_at :datetime
# updated_at :datetime
# like :integer default(0)
# email :string(255)
# level_cd :integer
#
class Girb < ActiveRecord::Base
has_many :bangs, dependent: :destroy
has_many :comments, dependent: :destroy
has_one :xfile
as_enum :level, dead: 0, successful: 1, famous: 2, working: 3, average: 4, amateur: 5, unknown: 6, bad: 7
def self.search(query)
where("name like ?", "%#{query}%")
end
end
app/models/xfile.rb
# == Schema Information
#
# Table name: xfiles
#
# id :integer not null, primary key
# girb_id :integer
# character :string(255)
# created_at :datetime
# updated_at :datetime
#
class Xfile < ActiveRecord::Base
belongs_to :girb
end
新增一個 app/models/girb_update_form.rb
app/models/girb_update_form.rb
class GirbUpdateForm
include ActiveModel::Model
attr_reader :girb
attr_reader :xfile
attr_accessor :name # from girb
attr_accessor :like # from girb
attr_accessor :email # from girb
attr_accessor :level_cd # from girb
attr_accessor :character # from xfile
validates_presence_of :email, :name, :like, :level_cd
validates_presence_of :character
def initialize(girb_id)
@girb = Girb.find_by_id(girb_id)
@xfile = @girb.xfile || @girb.build_xfile
self.email = @girb.email
self.name = @girb.name
self.like = @girb.like
self.level_cd = @girb.level_cd
self.character = @xfile.character
end
def persisted?
@girb.persisted? && @xfile.persisted?
end
def update(params)
self.email = params[:email]
self.name = params[:name]
self.like = params[:like]
self.level_cd = params[:level_cd]
self.character = params[:character]
if valid?
@girb.update_attributes(email: email, name: name, like: like, level_cd: level_cd)
@xfile.update_attributes(character: character)
true
else
false
end
end
end
app/controllers/girbs_controller.rb
class GirbsController < ApplicationController
@@get_level_cd = -1
@@get_search = ""
before_action :has_level, :has_search
def index
if @@get_search != ""
if params[:tab].present?
@girbs = Girb.search(@@get_search).order("name #{params[:tab]}").paginate(page: params[:page], per_page: 12)
else
@girbs = Girb.search(@@get_search).paginate(page: params[:page], per_page: 12)
end
else
if params[:level_cd].present?
@girbs = Girb.all.where(level_cd: @@get_level_cd).paginate(page: params[:page], per_page: 12)
else
if params[:tab].present?
@girbs = Girb.all.where(level_cd: @@get_level_cd).order("name #{params[:tab]}").paginate(page: params[:page], per_page: 12)
else
@girbs = Girb.all.paginate(page: params[:page], per_page: 12)
end
end
end
end
# def index
# @tab = if %w(asc desc).include?(params[:tab]) then params[:tab] else :asc end
# @girbs = Girb.level_list(params[:level_cd]).search(params[:search]).order(name: @tab)
# end
def like_asc
@at_like = false
@girbs = Girb.all.order("like ASC").paginate(page: params[:page], per_page: 12)
render :index
end
def like_desc
@at_like = true
@girbs = Girb.all.order("like DESC").paginate(page: params[:page], per_page: 12)
render :index
end
def like
@girb = Girb.find(params[:id])
@girb.like += 1;
@girb.save
# redirect_to(girbs_path)
redirect_to :back
end
def on_click_bang
@girb = Girb.find(params[:id])
@girb.bangs.create
# redirect_to(girbs_path)
redirect_to :back
end
def new
@girb = Girb.new
end
def create
@girb = Girb.new(girb_params)
if @girb.save
redirect_to(girbs_path)
else
render :new
end
end
def show
@girb = Girb.find(params[:id])
# @comment = @girb.comments.build
end
def edit
# @girb = Girb.find(params[:id])
@girb_update_form = GirbUpdateForm.new(params[:id])
end
def update
# @girb = Girb.find(params[:id])
@girb_update_form = GirbUpdateForm.new(params[:id])
# if @girb.update(girb_params)
if @girb_update_form.update(girb_params)
redirect_to girbs_path
else
render :edit
end
end
def destroy
@girb = Girb.find(params[:id])
@girb.destroy
redirect_to girbs_path
end
private
def girb_params
# params.require(:girb).permit(:name, :email, :like, :level_cd)
params.require(:girb_update_form).permit(:name, :email, :like, :level_cd, :character)
end
def has_level
if params[:level_cd].present?
if(nil != params[:level_cd].present?)
@@get_level_cd = params[:level_cd]
@@get_search = ""
end
end
@@get_level_cd
end
def has_search
if params[:search].present?
if(nil != params[:search].present?)
@@get_search = params[:search]
@@get_level_cd = -1
end
end
@@get_search
end
end
app/views/girbs/new.html.erb
<div class="container">
<%= form_for(@girb_update_form, url: girb_path(@girb_update_form.girb), :html => {:class => "form-horizontal center"}) do |f| %>
<div class="form-group">
<%= f.label :name, "Name:", class: "col-md-3 control-label" %>
<div class="col-md-7">
<%= f.text_field :name, class: "form-control" %>
</div>
</div>
<div class="form-group">
<%= f.label :email, "Email:", class: "col-md-3 control-label" %>
<div class="col-md-7">
<%= f.text_field :email, class: "form-control" %>
</div>
</div>
<div class="form-group">
<%= f.label :level, "Level:", class: "col-md-3 control-label" %>
<div class="col-md-7">
<%= f.collection_select :level_cd, Girb.all.select(:level_cd).distinct, :level_cd, :level, include_blank: false %>
</div>
</div>
<div class="form-group">
<%= f.label :like, "Like:", class: "col-md-3 control-label" %>
<div class="col-md-7">
<%= f.number_field :like, class: "form-control" %>
</div>
</div>
<div class="form-group">
<%= f.label :character, "Character:", class: "col-md-3 control-label" %>
<div class="col-md-7">
<%= f.text_field :character, class: "form-control" %>
</div>
</div>
<div class="form-group">
<div class="col-md-3 control-label">
</div>
<div class="col-md-7">
<%= f.submit "Submit", class: "btn btn-default btn-primary" %>
</div>
</div>
<% end %>
</div>