简体   繁体   English

如何修改rails控制器中的参数

[英]How to modify the params in rails controller

Controller has to modify the session_id before saving the information. Controller必须在保存信息之前修改session_id。

user.rb
    has_one :room

class Room < ApplicationRecord
    belongs_to :user, optional: true
end

Rooms_controller.rb 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. 但这不起作用,session_id不存储在房间行中。

Question 1: How to modify 'rooms_params'? 问题1:如何修改'rooms_params'? Question 2: How to pass it to rooms model in the line 'current_user.create_room'? 问题2:如何将其传递到'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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM