Ruby on rails 验证后:在RubyonRails中设置\u全名
问题: 我正在制作一个老年护理应用程序。我有两种型号的Ruby on rails 验证后:在RubyonRails中设置\u全名,ruby-on-rails,Ruby On Rails,问题: 我正在制作一个老年护理应用程序。我有两种型号的房间和区域。验证后,我在房间表上创建了一个名为full_name的列,我想保存全名,这将用作友好的标签显示 我会在住院表格上用这个 <div class="col-md-4"> <%= f.association :room, collection: @current_business.rooms.order(:name).pluck(:full_name, :id), prompt: "Choose a roo
房间
和区域
。验证后,我在房间表上创建了一个名为full_name的列,我想保存全名,这将用作友好的标签显示
我会在住院表格上用这个
<div class="col-md-4">
<%= f.association :room, collection: @current_business.rooms.order(:name).pluck(:full_name, :id), prompt: "Choose a room" %>
</div>
Room.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
#==架构信息
#
#表名:房间
#
#id:bigint(8)不为空,主键
#名称:string
#创建时间:datetime非空
#更新时间:datetime非空
#区域id:bigint(8)
#业务标识:整数
#作者id:integer
#删除时间:datetime
#全名:string
#
教室<申请记录
表现得像个妄想狂
验证后:设置\u全名\u
验证:名称,状态:true,长度:{最大值:50}
属于:区域
有很多:居民
def set_全名
如果@room.name.changed?|@area.name\u更改了吗?
self.full_name=[@room.name,@area.name]。选择(&:present?).join(“”)。titleize
结束
结束
结束
Area.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
#==架构信息
#
#表名:区域
#
#id:bigint(8)不为空,主键
#名称:string
#创建时间:datetime非空
#更新时间:datetime非空
#位置标识:bigint(8)
#业务标识:整数
#作者id:integer
#删除时间:datetime
#
类区域<应用程序记录
表现得像个妄想狂
属于:地点
属于:商业
验证:名称,状态:true,长度:{最大值:100}
有多个:房间,->{order(id::asc)},面积的倒数
为:房间接受\u嵌套的\u属性\u,如果::all\u为空则拒绝\u,允许\u销毁:true
结束
房间控制器
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
class App::RoomsControllerparams[:page])
结束
#获取/房间/1
#GET/rooms/1.json
def秀
结束
#获得/房间/新
def新
@房间=当前的\u business.rooms.new
结束
#获取/房间/1/编辑
定义编辑
结束
#邮递室
#POST/rooms.json
def创建
@房间=当前的业务.房间.新(房间参数)
@room.author\u id=当前用户id
回应待办事项|格式|
如果@room.save
format.html{重定向到app_room_路径(@room),注意:“room已成功创建”。}
format.json{render:show,status::created,location:@room}
其他的
format.html{render:new}
format.json{render json:@room.errors,status::unprocessable_entity}
结束
结束
结束
#修补/放置/房间/1
#PATCH/PUT/rooms/1.json
def更新
@room.author\u id=当前用户id
回应待办事项|格式|
如果@room.update(房间参数)
format.html{重定向到app_room_路径(@room),注意:'room已成功更新。}
format.json{render:show,status::ok,location:@room}
其他的
format.html{render:edit}
format.json{render json:@room.errors,status::unprocessable_entity}
结束
结束
结束
#删除/rooms/1
#删除/rooms/1.json
def销毁
@毁灭
回应待办事项|格式|
format.html{重定向到app_rooms_路径,注意:'房间已成功销毁。}
format.json{head:no_content}
结束
结束
私有的
#使用回调在操作之间共享公共设置或约束。
def设置室
@room=当前_business.rooms.find(参数[:id])
结束
#永远不要相信来自恐怖网络的参数,只允许白名单通过。
def房间参数
参数require(:room).permit(:name,:area_id,:deleted_at,:business_id,:author_id,:business_id,:author_id,: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
教室
您已经在房间
和区域
上验证了名称
,那么为什么要验证所有这些。选择(&:present?
等业务?只需使用字符串插值,然后标题化
这样,全名将始终基于当前值,您不需要在验证后使用
进行“刷新”,诸如此类 我找到了另一个解决办法
def set_full_name
self.full_name = self.name
self.full_name += ', ' + self.area.name if self.area
end
为什么不把全名
作为一种方法?@jvillian你能进一步解释一下吗?我想你是对的,但我不明白
def set_full_name
self.full_name = self.name
self.full_name += ', ' + self.area.name if self.area
end