[英]How to modify the params in rails controller
Controller必須在保存信息之前修改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不存儲在房間行中。
問題1:如何修改'rooms_params'? 問題2:如何將其傳遞到'current_user.create_room'行中的房間模型? 謝謝
以這種方式定義
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.