Controller has to modify the session_id before saving the information.
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}'}
But this is not working, the session_id is not stored in the rooms row.
Question 1: How to modify 'rooms_params'? Question 2: How to pass it to rooms model in the line 'current_user.create_room'? Thank you
Define in this way
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.