在保存信息之前,控制器必须修改session_id。
user.rb
has_one :room
class Room < ApplicationRecord
belongs_to :user, optional: true
end
Rooms_controller.rb
n_room = room_params
n_room[:room][:session_id] = session.session_id
respond_to do |format|
binding.pry
if current_user.create_room(params[:n_room])
format.html { redirect_to '/rooms/#{@new_room.id}'}
但这不起作用,session_id不会存储在rooms行中。
问题1: 如何修改“ rooms_params”? 问题2: 如何将其传递给“ current_user.create_room”行中的房间模型? 谢谢
答案 0 :(得分:3)
以这种方式定义
def create
params[:room][:session_id] = 1
respond_to do |format|
if current_user.create_room(room_params)
format.html { redirect_to '/rooms/', notice: 'Room was successfully created.' }
else
format.html { render :new }
end
end
def room_params
params.require(:room).permit(:name, :type, :session_id)
end