代码之家  ›  专栏  ›  技术社区  ›  Ben Strachan

验证后:在rubyonrails中设置\u全名

  •  0
  • Ben Strachan  · 技术社区  · 6 年前

    我在做一个老年护理应用程序。我有两个模型 Room & Area . 我已经在文件室表上创建了一个名为full\ u name的列,在验证之后,我想保存全名,这将用作友好的标签显示

    我会在住院表格上用这个

      <div class="col-md-4">
        <%= f.association :room, collection: @current_business.rooms.order(:name).pluck(:full_name, :id), prompt: "Choose a room"   %>
      </div>
    

    我试过的:

    我认为问题出在这一部分:

     def set_full_name
        if @room.name.changed? || @area.name_changed?
          self.full_name = [@room.name, @area.name].select(&:present?).join(' ').titleize
        end
      end
    

    房间.rb

    # == Schema Information
    #
    # Table name: rooms
    #
    #  id          :bigint(8)        not null, primary key
    #  name        :string
    #  created_at  :datetime         not null
    #  updated_at  :datetime         not null
    #  area_id     :bigint(8)
    #  business_id :integer
    #  author_id   :integer
    #  deleted_at  :datetime
    #  full_name   :string
    #
    
    class Room < ApplicationRecord
      acts_as_paranoid
    
      after_validation :set_full_name
    
      validates :name, presence: true, length: { maximum: 50 }
      belongs_to :area
      has_many :residents
    
      def set_full_name
        if @room.name.changed? || @area.name_changed?
          self.full_name = [@room.name, @area.name].select(&:present?).join(' ').titleize
        end
      end
    
    end
    

    面积.rb

    # == Schema Information
    #
    # Table name: areas
    #
    #  id          :bigint(8)        not null, primary key
    #  name        :string
    #  created_at  :datetime         not null
    #  updated_at  :datetime         not null
    #  location_id :bigint(8)
    #  business_id :integer
    #  author_id   :integer
    #  deleted_at  :datetime
    #
    
    class Area < ApplicationRecord
      acts_as_paranoid
      belongs_to :location
      belongs_to :business
      validates :name, presence: true, length: { maximum: 100 }
    
      has_many :rooms, -> { order(id: :asc) }, inverse_of: :area
      accepts_nested_attributes_for :rooms, reject_if: :all_blank, allow_destroy: true
    
    
    
    end
    

    房间控制器

    class App::RoomsController < App::BaseController
      before_action :set_room, only: [:show, :edit, :update, :destroy]
    
      # GET /rooms
      # GET /rooms.json
      def index
        @rooms = current_business.rooms.order(name: :asc).paginate(:page => params[:page])
      end
    
      # GET /rooms/1
      # GET /rooms/1.json
      def show
      end
    
      # GET /rooms/new
      def new
        @room = current_business.rooms.new
      end
    
      # GET /rooms/1/edit
      def edit
      end
    
      # POST /rooms
      # POST /rooms.json
      def create
        @room = current_business.rooms.new(room_params)
        @room.author_id = current_user.id
    
        respond_to do |format|
          if @room.save
            format.html { redirect_to app_room_path(@room), notice: 'Room was successfully created.' }
            format.json { render :show, status: :created, location: @room }
          else
            format.html { render :new }
            format.json { render json: @room.errors, status: :unprocessable_entity }
          end
        end
      end
    
      # PATCH/PUT /rooms/1
      # PATCH/PUT /rooms/1.json
      def update
        @room.author_id = current_user.id
    
        respond_to do |format|
          if @room.update(room_params)
            format.html { redirect_to app_room_path(@room), notice: 'Room was successfully updated.' }
            format.json { render :show, status: :ok, location: @room }
          else
            format.html { render :edit }
            format.json { render json: @room.errors, status: :unprocessable_entity }
          end
        end
      end
    
      # DELETE /rooms/1
      # DELETE /rooms/1.json
      def destroy
        @room.destroy
        respond_to do |format|
          format.html { redirect_to app_rooms_path, notice: 'Room was successfully destroyed.' }
          format.json { head :no_content }
        end
      end
    
      private
        # Use callbacks to share common setup or constraints between actions.
        def set_room
          @room = current_business.rooms.find(params[:id])
        end
    
        # Never trust parameters from the scary internet, only allow the white list through.
        def room_params
          params.require(:room).permit(:name, :area_id, :deleted_at, :business_id, :author_id, :business_id, :author_id, :full_name)
        end
    end
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   jvillian    6 年前

    为什么不做一个 full_name 方法。比如:

    class Room < ApplicationRecord
      acts_as_paranoid
    
      validates :name, presence: true, length: { maximum: 50 }
      belongs_to :area
      has_many :residents
    
      def full_name
        "#{name} #{area.name}".titleize
      end
    
    end
    

    你已经验证了 name 两边都有 Room Area ,那为什么要这么做 .select(&:present?) 什么生意?只需使用字符串插值,然后标题化。

    after_validation 等等,等等。

        2
  •  0
  •   Ben Strachan    6 年前

    我找到了另一个解决办法

      def set_full_name
        self.full_name = self.name
        self.full_name += ', ' + self.area.name if self.area
      end