1

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

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

7 Comments

That seems to work except that the insert into statement is not storing the 'session_id' and 'name' value to the "Room" table. Do you the reason? INSERT INTO "rooms" ("created_at", "updated_at", "user_id") VALUES ($1, $2, $3) RETURNING "id" [["created_at", "2018-12-25 17:18:14.300732"], ["updated_at", "2018-12-25 17:18:14.300732"], ["user_id", 6]] Thanks
Rooms table has all "session_id" and "name" column:
It worked! I gave current_user.create_room(params[room_params]) instead of current_user.create_room(room_params)
That is an example for your questions but those attribute i decalred inside for my room model. First u need to declare in your db. If u liked my question than please accept the answer
You can follow this links for strong params edgeapi.rubyonrails.org/classes/ActionController/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.