简体   繁体   中英

How can i use the param id in params_permit?

How can i put the id inside of my params?

so far i am doing like this:

def post_params
        params.require(:post).permit(:name, :country, user_id:params[:user_id], live_id: live.id)
end

I want to put params[:user_id] and live.id inside of params

You can set live id from your controller actions instead of using param method. Please follow below steps. Hope it will be useful for you.

def create
  live = Live.create()
  
  # Set live id inside post_params
  @post = Post.new(post_params.merge!(live_id: live.id))

  # If you have current_user present you can set user_id as well using below way
  # @post = Post.new(post_params.merge!(user_id: current_user.id, live_id: live.id))
   

  respond_to do |format|
    if @post.save
      format.html { redirect_to @post, notice: 'Post was successfully 
                                           created.' }
      format.json { render :show, status: :created, location: 
                   @post }
    else
      format.html { render :new }
      format.json { render json: @post.errors, status: 
                     :unprocessable_entity }
    end
  end
end

def post_params
    params.require(:post).permit(:name, :country, :user_id, :live_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.

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