Adding variable to params in rails

后端 未结 3 438
迷失自我
迷失自我 2020-12-13 17:50

How can i add user_id into params[:page] i don\'t want to use hidden fields.

@page= Page.new(params[:page])

Is there a way to use like

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-13 18:43

    Instead of trying to merge the current_user id, you can build the proper model associations and then scope the new() method

    Models

    class Page < ActiveRecord::Base
     belongs_to :user
    end
    
    class User < ActiveRecord::Base
     has_many :pages
    end
    

    Controller

    if current_user
     @page = current_user.pages.new(params[:page])
    else
     @page = Page.new(params[:page])
    end
    

提交回复
热议问题